Java SMS platform to realize the function of texting
The use of texting in the project, but that due to the company's internal constraints is 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 for registered users, 3 free MMS, which is enough for our test use.) 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 * * * * * * * * * * * 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 {/** * @author dengsilinming * @date Sep, * @time 9:38:25 AM * @param args * @throws IOException * @throws httpexception * @description/public static void main (string[] args) throws Httpex
Ception, 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.addrequestheader ("Content-type", "APPLICATION/X-WWW-FORM-URLENCODED;CHARSET=GBK");//Set transcoding in header file NameVal uepair[] data = {new Namevaluepair ("Uid", "DenGsilinming "),//Registered username new Namevaluepair (" Key "," 72da78da5ff54f450505 "),//Registration after the successful login site after the key to the new Namevaluepair (" Smsmob "," 12345678900 "),//Mobile phone number new Namevaluepair (" Smstext "," This is dedicated to testing information, can normal text messages? ")
")};//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);
}
}
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 is excerpted from the Chinese build network SMS Messaging API:
GBK encoding Send interface address:
Http://gbk.sms.webchinese.cn/?Uid= site username &key= interface Security Password &smsmob= mobile phone number &smstext= SMS content
UTF-8 encoding Send interface address:
Http://utf8.sms.webchinese.cn/?Uid= site username &key= interface Security Password &smsmob= mobile phone number &smstext= SMS content
Get SMS Quantity Interface Address (UTF8):
http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid= site user name &key= interface security
Get SMS Quantity Interface Address (GBK):
Http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid= site username &key= interface Security password
Tip: When HTTP invokes the URL interface, the parameter value must be URL-coded before calling the
Multiple cell phone number please use half-width, separated, such as: 13888888886,13888888887,1388888888 at one time up to 50 phone send
Short message content support long letter, up to 300 words, ordinary SMS 70 words/article, Long Letter 64 Word/Section Billing
The following are simple demos that invoke the SMS interface in different languages:
1. ASP calls the
<%
' Common functions
' Enter URL destination page address, return value Gethttppage is the HTML code function of the target page
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. The close
Set objstream = no end
Function
' combines the submitted 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
The namespace using system.net that needs to be used
;
Using System.IO;
Using System.Text;
You only need to pass the spelled URL to the function when calling. The return value can be judged by 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 = * 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 Ht
Tpclient ();
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 username"), 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 Call
$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
' Call send SMS, nolist receive number. Multiple, separate, memo content 70 words
Public Function sendsms (ByVal nolist As String, ByVal Memo as String) As String
Dim Url As String = "Http://sms.webchi Nese.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 Java source code is able to send a successful, other languages are not tested.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.