Today saw a demand, need to do a text messaging function, online search, there are probably these methods: (1) Use the WebService interface to send SMS, this can use the webservice provided by Sina to send, but need to register; (2) Use text message Mao's way to send text messages, this way should be more commonly used, the premise is the need to buy hardware equipment, hehe (3) using the Chinese network built SMS platform (application account address: http://sms.webchinese.com.cn/), Heard that the network built to provide a few free text messages, went up to try, called the next his interface (the actual call interface, is actually a copy of a code, hehe ~), the code as follows, and you share:
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 {
public static void Main (string[] args) throws Exception {
HttpClient client = new HttpClient ();
Postmethod post = new Postmethod ("http://gbk.sms.webchinese.cn");
Post.addrequestheader ("Content-type",
"APPLICATION/X-WWW-FORM-URLENCODED;CHARSET=GBK");//Set transcoding in header file
namevaluepair[] data = {new Namevaluepair ("Uid", "Registered user name"),
New Namevaluepair ("Key", "key is not registered password"),
New Namevaluepair ("Smsmob", "Send phone number * * * * * * * * *"),
SMS must be a complete SMS (Details Web site) can be issued. The return has been sent successfully, but the phone has not received a text message,
Suddenly the network built to the network construction staff of the telephone, told me the reason, or good. Too much nonsense, hehe ~ ~
New Namevaluepair ("Smstext", "Sent SMS")};
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); Print return message status
Post.releaseconnection ();
}
}
Required JAR Packages
commons-codec-1.4
Commons-httpclient-3.1.jar
Commons-logging-1.1.1.jar
More
How Java implements sending text messages