1. 程式人生 > >java 實現簡單的短信發送

java 實現簡單的短信發送

下載 har ase 設置 request enc value pri ring

現在中國網建上註冊一個自己的賬戶,

然後裏面有代碼案例,也有相應的下載jar包的地址

技術分享

代碼如下:

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");
NameValuePair[] data = {
new NameValuePair("Uid", "theStar"),
new NameValuePair("Key", "73d1e45fd5f0fa2b9a87"),
new NameValuePair("smsMob", "18324121564"),
new NameValuePair("smsText", "驗證碼:6699")
};
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();
}
}

java 實現簡單的短信發送