1. 程式人生 > >java實現的一個傳送手機簡訊

java實現的一個傳送手機簡訊

今天閒來無事,在微博上看到一個關於用Java實現的一個傳送手機簡訊的程式,看了看,寫的不太相信,閒的沒事,把他整理下來,以後可能用得著

JAVA傳送手機簡訊,流傳有幾種方法:(1)使用webservice介面傳送手機簡訊,這個可以使用sina提供的webservice進行傳送,但是需要進行註冊;(2)使用簡訊mao的方式進行簡訊的傳送,這種方式應該是比較的常用,前提是需要購買硬體裝置,呵呵

本程式主要是運用了中國網建提供的SMS簡訊平臺,這個簡訊平臺基於java提供個專門的介面,話不多說。,上程式碼,有程式碼有真相,呵呵

[java] view plain copy  print
?
  1. package com.text;  
  2. import org.apache.commons.httpclient.Header;  
  3. import org.apache.commons.httpclient.HttpClient;  
  4. import org.apache.commons.httpclient.NameValuePair;  
  5. import org.apache.commons.httpclient.methods.PostMethod;  
  6. publicclass SendMsg_webchinese {  
  7.     publicstaticvoid main(String[] args) 
    throws Exception {  
  8.         HttpClient client = new HttpClient();  
  9.         PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");  
  10.         post.addRequestHeader("Content-Type",  
  11.                 "application/x-www-form-urlencoded;charset=gbk");// 在標頭檔案中設定轉碼
  12.         NameValuePair[] data = { new
     NameValuePair("Uid""cshxxxxxxxx"), // 註冊的使用者名稱
  13.                 new NameValuePair("Key""53295058d1c46710666a"), // 註冊成功後,登入網站使用的金鑰
  14.                 new NameValuePair("smsMob""187xxxxxxx"), // 手機號碼
  15.                 new NameValuePair("smsText""以後給我老實點哈。。。。聽話。。。") };//設定簡訊內容        
[java] view plain copy  print?
  1.     post.setRequestBody(data);  
  2.     client.executeMethod(post);  
  3.     Header[] headers = post.getResponseHeaders();  
  4.     int statusCode = post.getStatusCode();  
  5.     System.out.println("statusCode:" + statusCode);  
  6.     for (Header h : headers) {  
  7.         System.out.println(h.toString());  
  8.     }  
  9.     String result = new String(post.getResponseBodyAsString().getBytes(  
  10.             "gbk"));  
  11.     System.out.println(result);  
  12.     post.releaseConnection();  
  13. }  

執行本程式首先的代入三個jar包:

commons-codec-1.4

commons-httpclient-3.1

commons-logging-1.1.1

請自行下載,呵呵

GBK編碼傳送介面地址
http://gbk.sms.webchinese.cn/?Uid=本站使用者名稱&Key=介面安全密碼&smsMob=手機號碼&smsText=簡訊內容 
UTF-8編碼傳送介面地址:
http://utf8.sms.webchinese.cn/?Uid=本站使用者名稱&Key=介面安全密碼&smsMob=手機號碼&smsText=簡訊內容
獲取簡訊數量介面地址(UTF8):
http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid=本站使用者名稱&Key=介面安全密碼
獲取簡訊數量介面地址(GBK):
http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid=本站使用者名稱&Key=介面安全密碼

簡訊傳送後返回值 說 明
-1 沒有該使用者賬戶
-2 金鑰不正確(不是使用者密碼)
-3 簡訊數量不足
-11 該使用者被禁用
-14 簡訊內容出現非法字元
-41 手機號碼為空
-42 簡訊內容為空
大於0 簡訊傳送數量

注:上面的使用者名稱和密碼是我原先申請的,不知道為什麼被停用了,在執行本程式之前請先到SMS簡訊平臺去申請一個使用者名稱和密碼。

附:

1. ASP 呼叫例子
<%
'常用函式
'輸入url目標網頁地址,返回值getHTTPPage是目標網頁的html程式碼
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

'自已組合一下提交的URL加入自己的賬號和密碼
sms_url="http://sms.webchinese.cn/web_api/?Uid=賬號&Key=介面金鑰&smsMob=手機號碼&smsText=簡訊內容"
response.write getHTTPPage(sms_url)
%> 

2.C# 呼叫
//需要用到的名稱空間
using System.Net;
using System.IO;
using System.Text;
//呼叫時只需要把拼成的URL傳給該函式即可。判斷返回值即可
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呼叫

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");//在標頭檔案中設定轉碼
NameValuePair[] data ={ new NameValuePair("Uid", "本站使用者名稱"),new NameValuePair("Key", "介面安全密碼"),new NameValuePair("smsMob","手機號碼"),new NameValuePair("smsText","簡訊內容")};
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();

}

}

4.PHP
$url='http://sms.webchinese.cn/web_api/?Uid=賬號&Key=介面金鑰&smsMob=手機號碼&smsText=簡訊內容';

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
'呼叫傳送簡訊,NoList接收號碼.多個之間用,分開,Memo內容70字
Public Function SendSMS(ByVal NoList As String, ByVal Memo As String) As String 
Dim Url As String = "http://sms.webchinese.cn/web_api/?Uid=賬號&Key=介面金鑰&smsMob=手機號碼&smsText=簡訊內容"
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