A small example of using Java to send text messages

Source: Internet
Author: User

 

Today, I have nothing to worry about. on Weibo, I saw a program for sending text messages via Java. I looked at it and did not believe it. I am not sure about it. I will sort it out and may need it later.

There are several methods to send text messages via Java: (1) Use the WebService interface to send text messages, which can be sent using the WebService provided by Sina, but must be registered; (2) you can use the short message Mao method to send short messages. This method should be quite common, provided that you need to purchase hardware devices. (3) use the SMS platform provided by China Internet (apply for account address: http://sms.webchinese.cn/default.shtml)

This program mainly uses the SMS platform provided by China Internet. This SMS platform provides a special interface based on Java ., On the code, there is code with truth, huh, huh

Package COM. text; 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 "); // set transcoding namevaluepair [] DATA = {New namevaluepair ("uid", "cshxxxxxxxx") in the header file, // The registered username new namevaluepair ("key ", "53295058d1c46710666a"), // The New namevaluepair ("smsmob", "187 xxxxxxx") used by the website after successful registration, // the mobile phone number new namevaluepair ("smstext ", "Be honest with me later .... Obedient... ")}; // Set the text message content
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();}}

To run this program, enter the following three jar packages:

Commons-codec-1.4

Commons-httpclient-3.1

Commons-logging-1.1.1

Please download it by yourself.

GBK-encoded sending interface address:
Http://gbk.sms.webchinese.cn /? Uid = user name on this site & Key = API security password & smsmob = Mobile Phone Number & smstext = SMS content
UTF-8 code send interface address:

Http://utf8.sms.webchinese.cn /? Uid = user name on this site & Key = API security password & smsmob = Mobile Phone Number & smstext = SMS content
API address for getting the number of text messages (utf8 ):

Http://sms.webchinese.cn/web_api/SMS? Action = sms_num & uid = user name on this site & Key = interface Security Password
API address for getting the SMS quantity (GBK ):
Http://sms.webchinese.cn/web_api/SMS/GBK? Action = sms_num & uid = user name on this site & Key = interface Security Password

 

Returned value after the SMS is sent Description
-1 This user account does not exist
-2 Incorrect key (not user password)
-3 Insufficient SMS count
-11 This user is disabled
-14 The text message contains invalid characters.
-41 The mobile phone number is blank.
-42 The text message content is blank.
Greater than 0 Number of messages sent

Note: The above user name and password were originally applied for and I don't know why they were stopped. Before running this program, please apply for a user name and password on the SMS platform.

 

 

Appendix:

1. asp call example
<%
'Common Functions
'Enter the URL target webpage address. The returned value gethttppage is the HTML code of the target webpage.
Function gethttppage (URL)
Dim HTTP
Set HTTP = server. Createobject ("msxml2.xmlhttp ")
HTTP. Open "get", URL, false
HTTP. Send ()
If HTTP. readystate <> 4 then
Exit Function
End if
Gethttppage = bytestobstr (HTTP. responsebody, "gb2312 ")
Set HTTP = nothing
If err. Number <> 0 then err. Clear
End Function
Function bytestobstr (body, cset)
Dim objstream
Set objstream = server. Createobject ("ADODB. Stream ")
Objstream. type = 1
Objstream. mode = 3
Objstream. Open
Objstream. Write body
Objstream. Position = 0
Objstream. type = 2
Objstream. charset = cset
Bytestobstr = objstream. readtext
Objstream. Close
Set objstream = nothing
End Function

'Combine the submitted URL with your account and password.
Sms_url = "http://sms.webchinese.cn/web_api? Uid = Account & Key = interface key & smsmob = Mobile Phone Number & smstext = SMS content"
Response. Write gethttppage (sms_url)
%>

2. C # Call
// Namespace to be used
Using system. net;
Using system. IO;
Using system. text;
// You only need to pass the assembled URL to the function during the call. Determine the return value.
Public String gethtmlfromurl (string URL)
{
String strret = NULL;

If (url = NULL | URL. Trim (). tostring () = "")
{
Return strret;
}
String TargetUrl = URL. Trim (). tostring ();
Try
{
Httpwebrequest hR = (httpwebrequest) webrequest. Create (TargetUrl );
HR. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 )";
HR. method = "get ";
HR. Timeout = 30*60*1000;
Webresponse HS = HR. getresponse ();
Stream sr = HS. getresponsestream ();
Streamreader SER = new streamreader (Sr, encoding. Default );
Strret = Ser. readtoend ();
}
Catch (exception ex)
{
Strret = NULL;
}
Return strret;
}

3. Java call

Import java. Io. unsupportedencodingexception;
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://gbk.sms.webchinese.cn ");
Post. addrequestheader ("Content-Type", "application/X-WWW-form-urlencoded; charset = GBK"); // set transcoding in the header file
Namevaluepair [] DATA = {New namevaluepair ("uid", "user name on this site"), new namevaluepair ("key", "interface security password"), new namevaluepair ("smsmob ", "Mobile Phone Number"), new namevaluepair ("smstext", "SMS content ")};
Post. setrequestbody (data );

Client.exe cutemethod (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 ();

}

}

Download jar package
Commons-logging-1.1.1.jar
Commons-httpclient-3.1.jar
Commons-codec-1.4.jar

4. php
$ Url = 'HTTP: // sms.webchinese.cn/web_api /? Uid = Account & Key = interface key & smsmob = Mobile Phone Number & smstext = SMS content ';

Echo get ($ URL );
Function get ($ URL)
{
If (function_exists ('file _ get_contents '))
{
$ File_contents = file_get_contents ($ URL );
}
Else
{
$ CH = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, curlopt_url, $ URL );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_connecttimeout, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
}
Return $ file_contents;
}

5. VB. NET
'Send SMS by calling, nolist receiving number. Separate multiple numbers with 70 words for memo content
Public Function sendsms (byval nolist as string, byval memo as string) as string
Dim URL as string = "http://sms.webchinese.cn/web_api? Uid = Account & Key = interface key & smsmob = Mobile Phone Number & smstext = SMS content"
Dim WebClient as new net. WebClient ()
Try
'Dim responsedata as byte () =
Dim srcstring as string = WebClient. downloadstring (URL)
Return srcstring
Catch
Return "-444"
End try
End Function

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.