Java implements the SMS message function through SMS SMS platform
I used the function of sending text messages in the project, but due to internal restrictions, I found a simple online today and recorded it as follows when I have nothing to do:
This program is implemented by using the SMS SMS platform provided by China Net Construction (the platform currently provides registered users with 5 free SMS and 3 free MMS, which is enough for us to test. You need to register before use, and the registration address is 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.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 18, 2012 * @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.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// Set transcoding NameValuePair[] data = { new NameValuePair("Uid", "dengsilinming"),// Registered username new NameValuePair("Key", "72da78da5ff54f450505"),// After successfully registering, the key obtained after logging in to the website is new NameValuePair("smsMob", "12345678900"),// Mobile phone number new NameValuePair("smsText", "This is a message dedicated to testing. Can you send text messages normally? ") };// 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); } } There are three jar packages to use:
commons-logging-1.1.1.jar
commons-httpclient-3.1.jar
commons-codec-1.4.jar
The following content is excerpted from China Jianshe SMS SMS API:
GBK encoding sending interface address:
http://gbk.sms.webchinese.cn/?Uid=User name of this site&Key=Interface security password&smsMob=Mobile number&smsText=SMS content
UTF-8 encoding sending interface address:
http://utf8.sms.webchinese.cn/?Uid=User name of this site&Key=Interface security password&smsMob=Mobile number&smsText=SMS content to obtain the number of SMS interface address (UTF8):
http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid=Username of this site&Key=Interface security and security obtain SMS number interface address (GBK):
http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid=Username of this site&Key=Interface security password
Tip: When HTTP calls the URL interface, the parameter value must be URL encoded before calling
Please use half a corner to separate multiple mobile phone numbers, such as: 13888888886, 1388888887, 138888888888888 Send text messages to up to 50 mobile phones at a time and support long text messages, up to 300 words, ordinary text messages 70 words/post, long text messages 64 words/post billing
Here is a simple demo of calling SMS interfaces in different languages:
1. ASP call
<% 'Common Function' Enter the URL destination web page address, and the return value getHTTPPage is the html code of the destination web 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 'You have combined the submitted URL to add your account and password to your own account and password sms_url="http://sms.webchinese.cn/web_api/?Uid=account&Key=interface key&smsMob=mobile number&smsText=sMS content" response.write getHTTPPage(sms_url) %> 2.C# call
//The namespace required using System.Net; using System.IO; using System.Text; // When calling, you only need to pass the spelled URL to the function. Just judge 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 NameValuePair[] data ={ new NameValuePair("Uid", "user name of this site"),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 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 to send SMS, NoList to receive the number. Use multiple times, separate, and 70 words of 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 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, but other languages have no tests.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.