本程序是經過使用中國網建提供的SMS短信平臺實現的(該平臺目前爲註冊用戶提供5條免費短信,3條免費彩信,這足夠用於咱們測試用了。在使用前須要註冊,註冊地址爲http://sms.webchinese.cn/reg.shtml)html
下面是實現發送短信的java源碼:java
package com.weixinsf.utils; /** * <p>Title: 短信發送 </p> * * <p>Description: 發送短信的工具類 </p> * * @author liufeng * @date 2016-9-5 * @version 1.0 */ 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 SendMsg_webchinese { 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");// 在頭文件中設置轉碼 NameValuePair[] data = { new NameValuePair("Uid", "用戶名"),//中國網建sms平臺註冊的用戶名 new NameValuePair("Key", "用戶密鑰"),//中國網建sms平臺註冊的用戶密鑰 new NameValuePair("smsMob", "13888888888"),//將要發送到的手機號碼 new NameValuePair("smsText", "驗證碼:280934") };//要發送的短信內容 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包web