Java implements the text messaging function through the SMS Platform

Source: Internet
Author: User

I used the text message function in the project, but it was very troublesome because of the company's internal restrictions. I found a simple one on the internet today and recorded it as follows when I had nothing to do with it:

This program is implemented by using the SMS platform provided by China Internet (this platform currently provides five free text messages and three free MMs for registered users, which is enough for our testing. Before using the need to register, registered address for http://sms.webchinese.cn/reg.shtml), the following is the program source code:

/*** @ Author dengsilinming * @ date 2012-9-18 ***/package COM. dengsilinming. mail; import Java. io. ioexception; import Org. apache. commons. httpclient. header; import Org. apache. commons. httpclient. httpclient; import Org. apache. commons. httpclient. httpexception; import Org. apache. commons. httpclient. namevaluepair; import Org. apache. commons. httpclient. methods. postmethod; public class sendmsg_webchinese {/*** @ aut Hor dengsilinming * @ date Sep 18,201 2 * @ time 9:38:25 am * @ Param ARGs * @ throws ioexception * @ throws httpexception * @ Description */public static void main (string [] ARGs) throws httpexception, ioexception {httpclient client = new httpclient (); postmethod post = new postmethod ("http://gbk.sms.webchinese.cn"); // postmethod post = new postmethod ("http://sms.webchinese.cn/web_api/"); Post. addrequesth Eader ("Content-Type", "application/X-WWW-form-urlencoded; charset = GBK "); // set transcoding namevaluepair [] DATA = {New namevaluepair ("uid", "dengsilinming") in the header file, // The registered username new namevaluepair ("key ", "72da78da5ff54f0000505"), // The key new namevaluepair ("smsmob", "12345678900") obtained after you log on to the website, // the mobile phone number new namevaluepair ("smstext ", "This is the test information. Can I send text messages normally? ")}; // The content of the SMS is post.setrequestbody(dataworkflow using 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 );}}

 

Three jar packages are required:
Commons-logging-1.1.1.jar
Commons-httpclient-3.1.jar
Commons-codec-1.4.jar

The following content is taken from the sms api of China's construction network:

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
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

Tip: When HTTP calls a URL interface, the parameter value must be URL encoded before being called.

Parameter variables Description
GBK Encoded URL Http://gbk.sms.webchinese.cn/
Utf-8 Encoded URL Http://utf8.sms.webchinese.cn/
UID User name of this site (if you do not have the user name of this site, please register first)
Key Interface security password entered during registration (you can change the security password on the user platform)
Smsmob Destination mobile phone number (Separate multiple mobile phone numbers with commas)
Smstext SMS content, up to 300 characters, 70 characters/text messages, 64 characters/text messages

Separate multiple mobile phone numbers with half width, for example, 13888888886,13888888887. A maximum of 50 mobile phone numbers can be sent at a time.
SMS content supports long text messages, up to 300 characters, 70 characters/text messages for common text messages, and 64 words/text messages for billing

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.
-4 Incorrect Mobile Phone Number Format
-41 The mobile phone number is blank.
-42 The text message content is blank.
Greater than 0 Number of messages sent


The following is a simple demo of calling the SMS interface in different languages:

 

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

After testing, the above Java source code can be successfully sent, and other languages are not tested. If you have a better way to send text messages, thank you!

Reprinted please note from
Http://blog.csdn.net/dengsilinming/article/details/7991865

Related Article

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.