java 使用site2SMS/way2sms在java中发送短信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16336524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send SMS in java using site2SMS/way2sms
提问by user27
The following code is to send sms in JAVA language,
下面的代码是用JAVA语言发送短信,
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class site2sms
{
//Replace your site2sms username and password below
static final String _userName = "username";
static final String _password = "password";
static final String _url = "http://smsapi.cikly.in/index.php";
//static final String _url = "http://smsapi.cikly.in/index.php";
//static final String _url = "http://www.site2sms.com/user/send_sms_next.asp";
static final String charset = "UTF-8";
//to build the query string that will send a message
private static String buildRequestString(String targetPhoneNo, String message) throws UnsupportedEncodingException
{
String [] params = new String [5];
params[0] = _userName;
params[1] = _password;
params[2] = message;
params[3] = targetPhoneNo;
params[4] = "site2sms";
String query = String.format("uid=%s&pwd=%s&msg=%s&phone=%s&provider=%s",
URLEncoder.encode(params[0],charset),
URLEncoder.encode(params[1],charset),
URLEncoder.encode(params[2],charset),
URLEncoder.encode(params[3],charset),
URLEncoder.encode(params[4],charset)
);
return query;
}
public static void sendMessage(String reciever, String message) throws Exception
{
System.out.println("hi!hello");
//To establish the connection and perform the post request
URLConnection connection = new URL(_url + "?" + buildRequestString(reciever,message)).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
//This automatically fires the request and we can use it to determine the response status
InputStream response = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(response));
//System.out.println(br);
System.out.println(br.readLine());
}
public static void main(String [] args) throws Exception
{
System.out.println("enter Mobile No:");
Scanner scanIn = new Scanner(System.in);
String testPhoneNo = scanIn.nextLine();
scanIn.close();
String testMessage = "Sending Messages From java is not too hard";
sendMessage(testPhoneNo,testMessage);
}
}
when I run the program, there is no compile errorand also no runtime error, but message is not delivered to respective number.
当我运行程序时,没有编译错误,也没有运行时错误,但是消息没有传递到相应的编号。
Pls help me, any suggestion can be accepted.
请帮助我,任何建议都可以接受。
Thanks and Regards,
谢谢并恭祝安康,
回答by javadev
It is written in comments in your class : Replace your site2sms username and password. I guess you have to subsrcibe for an account in smsapi.cikly.in
它写在您班级的评论中:替换您的 site2sms 用户名和密码。我猜你必须在 smsapi.cikly.in 订阅一个帐户