先在中國(guó)網(wǎng)建短信平臺(tái)上注冊(cè)自己的賬號(hào)信息
關(guān)于自動(dòng)發(fā)送的短信內(nèi)容可以在平臺(tái)上自定義模板形式
自簽名必須要有测摔,不然無(wú)法發(fā)送
所需要的三個(gè)jar包可以從中國(guó)網(wǎng)建平臺(tái)上下載匕累,如下
commons-codec-1.4.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
也可以通過(guò)maven來(lái)自動(dòng)下載,maven依賴代碼如下:
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
程序代碼如下
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 Test {
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");// 在頭文件中設(shè)置轉(zhuǎn)碼
NameValuePair[] data = { new NameValuePair("Uid", "自己填寫(xiě)平臺(tái)注冊(cè)名"),//中國(guó)網(wǎng)建sms平臺(tái)注冊(cè)的用戶名
new NameValuePair("Key", "自己填寫(xiě)密鑰"),//中國(guó)網(wǎng)建sms平臺(tái)注冊(cè)的用戶密鑰
new NameValuePair("smsMob", "填寫(xiě)要發(fā)送的手機(jī)號(hào)"),//將要發(fā)送到的手機(jī)號(hào)碼
new NameValuePair("smsText", "驗(yàn)證碼:3301") };//要發(fā)送的短信內(nèi)容
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); // 打印返回消息狀態(tài)
post.releaseConnection();
}
}