现在中国网建上注册一个自己的账户,
然后里面有代码案例,也有相应的下载jar包的地址
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();
}
}