【发布时间】:2015-06-25 15:17:36
【问题描述】:
我用 Java 制作了一个 Web 应用程序,我想将 OTP 发送到手机号码。的用户,如果他们忘记了密码。我想在不使用 GSM 调制解调器的情况下通过互联网发送免费短信。请给我它的代码
【问题讨论】:
-
请给我一个圣代冰淇淋。
-
@ArnonZilca 真的!
标签: java
我用 Java 制作了一个 Web 应用程序,我想将 OTP 发送到手机号码。的用户,如果他们忘记了密码。我想在不使用 GSM 调制解调器的情况下通过互联网发送免费短信。请给我它的代码
【问题讨论】:
标签: java
试试这个示例代码。如果返回402,则消息发送成功。如果返回408,则表示消息未发送。
public String sendSMS(String mob_No,String msg)
{
String result="408";
int TIMEOUT_VALUE = 500;
try{
String username = "enter your username";
String password = "Enter your password";
String senderid = "Enter your senderid";
String message = msg;//"new message system....!";
String mobileNo = mob_No ;
HttpURLConnection connection = null;
URL url = new URL("Enter the url");
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setFollowRedirects(true);
connection.setConnectTimeout(TIMEOUT_VALUE);
String smsservicetype = "singlemsg"; // For single message.
String query = "username=" + URLEncoder.encode(username)
+ "&password=" + URLEncoder.encode(password)
+ "&smsservicetype=" + URLEncoder.encode(smsservicetype)
+ "&content=" + URLEncoder.encode(message) + "&mobileno="
+ URLEncoder.encode(mobileNo) + "&senderid="
+ URLEncoder.encode(senderid);
connection.setRequestProperty("Content-length", String
.valueOf(query.length()));
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
// open up the output stream of the connection
DataOutputStream output = new DataOutputStream(connection
.getOutputStream());
// write out the data
int queryLength = query.length();
output.writeBytes(query);
// output.close();
// get ready to read the response from the cgi script
DataInputStream input = new DataInputStream(connection
.getInputStream());
String tempres="";
// read in each character until end-of-stream is detected
for (int c = input.read(); c != -1; c = input.read())
{System.out.print((char) c);
tempres+=String.valueOf((char) c);
}
result=tempres.substring(0, tempres.indexOf(","));
input.close();
System.out.println("Resp Code:" + connection.getResponseCode());
System.out.println("Resp Message:"
+ connection.getResponseMessage());
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("More than " + TIMEOUT_VALUE + " elapsed.");
}
return result;
}
【讨论】: