云信 短信发送 demo

时间:2021-05-17 18:32:41
package com.dataTaskListener;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

//全文模板发送
public class SendMsg{
public static String sendMSM(String phone,String number){
try {
HttpClient client
= new HttpClient();
PostMethod post
= new PostMethod("http://api.sms.cn/sms/");
post.addRequestHeader(
"Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码
NameValuePair[] data ={ new NameValuePair("ac", "send"),new NameValuePair("uid", ""),new NameValuePair("pwd", ""),new NameValuePair("mobile",phone),
                     new NameValuePair("encode","utf8"),new NameValuePair("template",""),new NameValuePair("content","{\"number\":\""+number+"\"}")}; 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("utf-8")); //返回信息
//System.out.println(result); //打印返回消息状态
post.releaseConnection();
return result;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}