java

java发送手机短信

2019-01-08  本文已影响0人  赶路人_3864

其实也就是调用一个http的接口而已
public static void main(String[] args) throws Exception {

HttpClient client = new HttpClient();

PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");

post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码

//uid对应用户名 key对应密钥 smsmob 要发送的手机号 smstext 发送信息

NameValuePair[] data = { new NameValuePair("Uid", "liumenglei"), new NameValuePair("Key", "d41d8cd98f00b204e980"),

new NameValuePair("smsMob", "18852931902"), new NameValuePair("smsText", "验证码:8888") };

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();

}

返回结果解释

上一篇下一篇

猜你喜欢

热点阅读