Java SMS messaging platform for texting

Source: Internet
Author: User

In the project to use the ability to send text messages, but that because the company's internal constraints are very troublesome, today on the Internet to find a simple, free to record it as follows:

This program is implemented by using the SMS platform provided by China Network (the platform currently provides 5 free SMS, 3 free MMS for registered users, which is enough for our testing. Need to register before use, registered address is http://sms.webchinese.cn/reg.shtml), below is the program source code:

/*** @Author dengsilinming * @Date 2012-9-18 **/ PackageCom.dengsilinming.mail;Importjava.io.IOException;ImportOrg.apache.commons.httpclient.Header;Importorg.apache.commons.httpclient.HttpClient;Importorg.apache.commons.httpclient.HttpException;ImportOrg.apache.commons.httpclient.NameValuePair;ImportOrg.apache.commons.httpclient.methods.PostMethod; Public classSendmsg_webchinese {/**     * @authordengsilinming * @date Sep, * @time 9:38:25 AM *@paramargs *@throwsIOException *@throwsHttpException * @description*/     Public Static voidMain (string[] args)throwsHttpException, IOException {HttpClient client=NewHttpClient (); Postmethod Post=NewPostmethod ("http://gbk.sms.webchinese.cn"); //Postmethod post = new Postmethod ("http://sms.webchinese.cn/web_api/");Post.addrequestheader ("Content-type",                "APPLICATION/X-WWW-FORM-URLENCODED;CHARSET=GBK");//set transcoding in the header filenamevaluepair[] data = {NewNamevaluepair ("Uid", "dengsilinming"),//Registered user name                NewNamevaluepair ("Key", "72da78da5ff54f450505"),//after successful registration, the key that is obtained after logging into the website                NewNamevaluepair ("Smsmob", "12345678900"),//Mobile phone number                NewNamevaluepair ("Smstext", "This is a test-specific information, can you send text messages normally?") ") };//SMS Contentpost.setrequestbody (data);        Client.executemethod (POST); Header[] Headers=post.getresponseheaders (); intStatusCode =Post.getstatuscode (); System.out.println ("StatusCode:" +StatusCode);  for(Header h:headers) {System.out.println ("---" +h.tostring ()); The String result=NewString (Post.getresponsebodyasstring (). GetBytes ("GBK"));    SYSTEM.OUT.PRINTLN (result); }}

A total of three jar packs are needed:
Commons-logging-1.1.1.jar
Commons-httpclient-3.1.jar
Commons-codec-1.4.jar


The following excerpt from the China Construction network SMS API:

GBK encoded Send interface address :
http://gbk.sms.webchinese.cn/?Uid= site user name &key= interface security Password &smsmob= mobile phone number &smstext= SMS content
UTF-8 encoded Send interface address:
http://utf8.sms.webchinese.cn/?Uid= site user name &key= interface security Password &smsmob= mobile phone number &smstext= SMS content
Get SMS Number Interface address (UTF8):
http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid= site user name &key= interface security
Get SMS Number Interface address (GBK):
http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid= site user name &key= interface security Password

Tip: When HTTP calls the URL interface, the parameter value must be URL-encoded before calling

parameter variable
GBK encoded URL http://gbk.sms.webchinese.cn/
utf-8 encoded URL http://utf8.sms.webchinese.cn/
uid site user name (if you do not have this site user name, please register first)
key Interface security password (can be changed to user platform to modify security password)
smsmob purpose mobile phone number (multiple phone numbers are separated by commas)
smstext SMS content, up to 300 words, ordinary SMS 70 words/bar, long and short letter 64 words/bar billing

Multiple mobile phone number please use half-width, separate, such as: 13888888886,13888888887,1388888888 up to 50 mobile phones at a time
SMS content support long and short letter, up to 300 words, ordinary SMS 70 words/bar, long and short letter 64 words/bar Billing

return value After text message is sent Description
-1 No such user account
-2 The key is incorrect (not a user password)
-3 Insufficient number of SMS messages
-11 The user is disabled
-14 Illegal characters appear in SMS content
-4 Cell phone number is not formatted correctly
-41 Cell phone number is empty
-42 SMS content is empty
Greater than 0 Number of SMS messages sent


The following is a simple demo of a different language calling SMS interface:

1. ASP Invocation Example
<%
' Common functions
' Enter URL destination page address, return value Gethttppage is the HTML code of the target page
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

' Self-assembled URL to add your own 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

Namespaces that need to be used
Using System.Net;
Using System.IO;
Using System.Text;
You only need to pass the URL of the spell to the function when you call it. The return value can be judged
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 header file
namevaluepair[] Data ={new Namevaluepair ("Uid", "Site user name"), New Namevaluepair ("Key", "Interface Security password"), New Namevaluepair ("Smsmob "," mobile number "), New Namevaluepair (" Smstext "," SMS 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 ();

}

}

Jar Package Download
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

' Call to send SMS, nolist receive number. Between multiple uses, separate, memo content 70 words
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 is able to send a successful, no test in other languages. If you have a better way to send text messages, please tell them, thank you!

Reprint please indicate from http://blog.csdn.net/dengsilinming/article/details/7991865

Java SMS messaging platform for texting

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.