註冊贈送 5 條免費短信,適合測試驗證。介面支援 GBK/UTF-8 兩種編碼,HTTP GET/POST 均可。
public class SmsSender {
private static final String API_URL = "https://gbk.sms.webchinese.cn"; // GBK 介面
private static final String UID = "your_uid";
private static final String KEY = "your_api_key";
public static String send(String mobile, String content) throws Exception {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(API_URL);
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
NameValuePair[] data = {
new NameValuePair("Uid", UID),
new NameValuePair("Key", KEY),
new NameValuePair("smsMob", mobile),
new NameValuePair("smsText", content)
};
post.setRequestBody(data);
client.executeMethod(post);
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
post.releaseConnection();
return result; // 回傳 "成功提交" 或錯誤代碼
}
}
依賴 JAR: commons-httpclient-3.1.jar、commons-codec-1.4.jar、commons-logging-1.1.1.jar。
| 項目 | 說明 |
|---|---|
| GBK 介面 | https://gbk.sms.webchinese.cn/?Uid=...&Key=...&smsMob=...&smsText=... |
| UTF-8 介面 | https://utf8.sms.webchinese.cn/?Uid=...&Key=...&smsMob=...&smsText=... |
| 查詢餘額 (UTF-8) | https://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid=...&Key=... |
| 查詢餘額 (GBK) | https://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid=...&Key=... |
| 多號碼 | 半形逗號分隔,單次最多 50 號 |
| 長簡訊 | 最多 300 字,70 字/條計費,長簡訊 64 字/條 |
注意: 參數值必須 URL 編碼後再請求。
Function getHTTPPage(url)
Set Http = Server.CreateObject("MSXML2.XMLHTTP")
Http.Open "GET", url, False
Http.Send()
If Http.ReadyState <> 4 Then Exit Function
getHTTPPage = BytesToBSTR(Http.ResponseBody, "GB2312")
Set Http = Nothing
End Function
sms_url = "https://sms.webchinese.cn/web_api/?Uid=帳號&Key=密鑰&smsMob=手機&smsText=內容"
Response.Write getHTTPPage(sms_url)
public string GetHtmlFromUrl(string url) {
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = "Mozilla/4.0"; req.Method = "GET"; req.Timeout = 30*60*1000;
using (var resp = req.GetResponse())
using (var stream = resp.GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.Default))
return reader.ReadToEnd();
}
$url = 'https://sms.webchinese.cn/web_api/?Uid=帳號&Key=密鑰&smsMob=手機&smsText=內容';
echo file_get_contents($url); // 或 curl
Public Function SendSMS(NoList, Memo)
Dim url = "https://sms.webchinese.cn/web_api/?Uid=帳號&Key=密鑰&smsMob=" & NoList & "&smsText=" & Memo
Dim client As New Net.WebClient()
Return client.DownloadString(url)
End Function
SmsService.send(phone, templateId, params),內部組裝內容、調用 HTTP、解析回應、記錄日誌、失敗重試。本文提供 Java 完整可運行代碼,並補充 ASP/C#/PHP/VB.NET 跨語言調用範例。核心要點:編碼一致(GBK/UTF-8)、參數 URL 編碼、HTTP POST 避免日誌洩露、回應碼解析、異步化與多通道備援。生產環境建議直接對接三大雲廠商 SDK,功能更豐富、穩定性更高。