首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 整合 SMS 短信平台發送驗證碼完整實作(多語言版)

     編輯:Java 相關知識

方案選型:中國網建 SMS API

註冊贈送 5 條免費短信,適合測試驗證。介面支援 GBK/UTF-8 兩種編碼,HTTP GET/POST 均可。

Java 核心實作(Apache HttpClient 3.x)

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。

API 介面規格速查

項目說明
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 編碼後再請求。

多語言調用範例

ASP (VBScript)

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)

C#

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();
}

PHP

$url = 'https://sms.webchinese.cn/web_api/?Uid=帳號&Key=密鑰&smsMob=手機&smsText=內容';
echo file_get_contents($url); // 或 curl

VB.NET

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

實務整合建議

  • 封裝 Service 介面: 定義 SmsService.send(phone, templateId, params),內部組裝內容、調用 HTTP、解析回應、記錄日誌、失敗重試。
  • 驗證碼邏輯: 隨機生成 6 位數字、Redis 存儲 5 分鐘、發送模板「您的驗證碼是 {code},5 分鐘內有效」。
  • 異步發送: 丟入 MQ/線程池,避免阻塞主流程。
  • 多通道備援: 整合阿里雲、騰訊雲、網建等多家,失敗自動切換。
  • 簽名/模板審核: 國內短信必須在運營商備案簽名與模板,上線前完成報備。

總結

本文提供 Java 完整可運行代碼,並補充 ASP/C#/PHP/VB.NET 跨語言調用範例。核心要點:編碼一致(GBK/UTF-8)、參數 URL 編碼、HTTP POST 避免日誌洩露、回應碼解析、異步化與多通道備援。生產環境建議直接對接三大雲廠商 SDK,功能更豐富、穩定性更高。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved