java implements sending SMS

  • 2020-05-07 19:41:39
  • OfStack

This paper mainly studied the JAVA language sending mobile phone, and Shared it with you for your reference. The specific content is as follows

JAVA sends text messages on mobile phones in several ways:

(1) SMS can be sent by webservice, which can be sent by webservice provided by sina, but registration is required;
(2) SMS is sent by SMS mao, which should be more commonly used. The premise is to buy hardware equipment.
(3) using the SMS short message platform provided by china.org.cn, my small demo is sent based on this line.  

java to send SMS


/**
*  instructions :java Realize sending mobile phone short message 
*  The author :aa00aa00
*/
package com.test.mobile;
 
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
 
public class SendMsg_webchinese {
 
public static void main(String[] args) throws Exception {
 
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
post.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=gbk");//  Transcoding is set in the header file 
NameValuePair[] data = { new NameValuePair("Uid", "*****"), //  The registered user name 
new NameValuePair("Key", "*******"), //  After successful registration , The key used to log into the site 
new NameValuePair("smsMob", "*********"), //  Mobile phone number 
new NameValuePair("smsText", "java A message sent by a program !!") };
post.setRequestBody(data);
 
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:" + statusCode);
for (Header h : headers) {
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes(
"gbk"));
System.out.println(result);
post.releaseConnection();
}
}
 
 

Run the above code: you can send SMS to your mobile phone, I pro test, no problem, share to everyone!

The above is the entire content of this article, I hope to help you learn java programming.


Related articles: