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