java實現的一個傳送手機簡訊
今天閒來無事,在微博上看到一個關於用Java實現的一個傳送手機簡訊的程式,看了看,寫的不太相信,閒的沒事,把他整理下來,以後可能用得著
JAVA傳送手機簡訊,流傳有幾種方法:(1)使用webservice介面傳送手機簡訊,這個可以使用sina提供的webservice進行傳送,但是需要進行註冊;(2)使用簡訊mao的方式進行簡訊的傳送,這種方式應該是比較的常用,前提是需要購買硬體裝置,呵呵
本程式主要是運用了中國網建提供的SMS簡訊平臺,這個簡訊平臺基於java提供個專門的介面,話不多說。,上程式碼,有程式碼有真相,呵呵
[java] view plain copy print- package com.text;
- 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;
- publicclass SendMsg_webchinese {
-
publicstaticvoid main(String[] args)
- HttpClient client = new HttpClient();
- PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
- post.addRequestHeader("Content-Type",
- "application/x-www-form-urlencoded;charset=gbk");// 在標頭檔案中設定轉碼
-
NameValuePair[] data = { new
- new NameValuePair("Key", "53295058d1c46710666a"), // 註冊成功後,登入網站使用的金鑰
- new NameValuePair("smsMob", "187xxxxxxx"), // 手機號碼
- 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();
- }
執行本程式首先的代入三個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 呼叫例子 |
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()=="") |
3.JAVA呼叫
import java.io.UnsupportedEncodingException;
public class SendMsg_webchinese { } |
4.PHP $url='http://sms.webchinese.cn/web_api/?Uid=賬號&Key=介面金鑰&smsMob=手機號碼&smsText=簡訊內容';
echo Get($url); |
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 |