本文基於某短信服務商(如 smschinese.cn)的 HTTP API 接口,提供五種主流語言的短信發送調用示例。核心流程:註冊賬號 → 獲取 Uid/Key → 拼接 URL/POST 請求 → 解析返回碼。
<%
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
getHTTPPage = BytesToBSTR(Http.ResponseBody, "GB2312")
Set Http = Nothing
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
End Function
sms_url = "https://sms.webchinese.cn/web_api/?Uid=賬號&Key=密鑰&smsMob=手機號&smsText=內容"
Response.Write getHTTPPage(sms_url)
%>
using System.Net;
using System.IO;
using System.Text;
public string GetHtmlFromUrl(string url) {
if (string.IsNullOrWhiteSpace(url)) return null;
try {
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
req.Method = "GET";
req.Timeout = 30 * 60 * 1000;
using (WebResponse resp = req.GetResponse())
using (Stream stream = resp.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.Default)) {
return reader.ReadToEnd();
}
} catch { return null; }
}
// 調用
string url = "https://sms.webchinese.cn/web_api/?Uid=賬號&Key=密鑰&smsMob=手機號&smsText=內容";
string result = GetHtmlFromUrl(url);
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
public class SendSms {
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("https://gbk.sms.webchinese.cn");
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
post.setRequestBody(new NameValuePair[] {
new NameValuePair("Uid", "用戶名"),
new NameValuePair("Key", "接口密鑰"),
new NameValuePair("smsMob", "手機號"),
new NameValuePair("smsText", "短信內容")
});
client.executeMethod(post);
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println("狀態碼: " + post.getStatusCode());
System.out.println("返回: " + result);
post.releaseConnection();
}
}
$url = 'https://sms.webchinese.cn/web_api/?Uid=賬號&Key=密鑰&smsMob=手機號&smsText=短信內容';
function httpGet($url) {
if (function_exists('file_get_contents')) {
return file_get_contents($url);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo httpGet($url);
Public Function SendSMS(NoList As String, Memo As String) As String
Dim Url As String = "https://sms.webchinese.cn/web_api/?Uid=賬號&Key=密鑰&smsMob=" & NoList & "&smsText=" & Memo
Dim client As New Net.WebClient()
Try
Return client.DownloadString(Url)
Catch
Return "-444"
End Try
End Function
短信發送本質是 HTTP API 調用,各語言核心差異僅在 HTTP 客戶端庫用法。掌握一語言精髓,舉一反三輕鬆移植。