短信接口都是这样做的

来源:互联网 发布:csgo皮肤测评 知乎 编辑:程序博客网 时间:2024/05/10 15:51
public void send(String phoneNo, String content){

boolean flag = false;
HttpClient client = new HttpClient();
GetMethod method = new GetMethod();
try {
URI base;
try {
base = new URI("短信平台地址", false);
method.setURI(new URI(base, "HttpBatchSendSM", false));
method.setQueryString(new NameValuePair[] { 
new NameValuePair("account", "短信平台账号"),

new NameValuePair("pswd", "短信平台密码"), 

//其他的参数看文档

new NameValuePair("mobile", phoneNo),
new NameValuePair("needstatus", String.valueOf(true)), 
new NameValuePair("msg", content),
new NameValuePair("product", null),
new NameValuePair("extno", null), 
});
} catch (URIException | NullPointerException e) { 
e.printStackTrace();
}
int result;
try {
result = client.executeMethod(method);
if (result == HttpStatus.SC_OK) {
InputStream in = method.getResponseBodyAsStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String[] str=URLDecoder.decode(baos.toString(), "UTF-8").split(",");
String[] s=str[1].split("\\r?\\n");
if(s[0].equals("0"))
{
flag=true;
}
return flag;

/*else {
throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
}*/
} catch (IOException e) { 
e.printStackTrace();
}
} finally {
method.releaseConnection();
}
return flag;
}
0 0
原创粉丝点击