如今中國網建上註冊一個本身的帳戶,java
而後裏面有代碼案例,也有相應的下載jar包的地址apache
package com.direct.note; import java.io.IOException; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public class Message { public static void main(String[] args) { try { new Message().sendMessage(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void sendMessage() throws HttpException, IOException{ HttpClient client = new HttpClient(); PostMethod post = new PostMethod("http://gbk.api.smschinese.cn"); //在頭文件中設置轉碼 post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk"); int count = (int)(Math.random()*10000)+1; System.out.println(count); String message = "驗證碼:"+count; NameValuePair[] data = { new NameValuePair("Uid", "theStar"), new NameValuePair("Key", "你的key"),//73d1e45fd5f0fa2b9a87 new NameValuePair("smsMob", "發送的電話號碼"), new NameValuePair("smsText", message) }; post.setRequestBody(data); client.executeMethod(post); Header[] headers = post.getRequestHeaders(); 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(); } }