满友科技短信接口平台调用代码

几经周转,终于把get方式,改成了post方式,期间出了几个叉子,现在把出现问题的地方特别提出来,以免以后码友们出现类似的问题

除了PostMethod method = new PostMethod(); 之外method.setRequestBody (new NameValuePair[] {.... });这里也是个问题,get方式用的方法是method.setRequestHeader (new NameValuePair[] { });

话不多说,看代码

protected static   String  URL="http://sdk.shmyt.cn:8080/manyousms/";//应用地址

     @SuppressWarnings("deprecation")
public static String batchSend(String url, String account, String pswd, String mobile, String msg,
boolean needstatus, String product, String extno) throws Exception {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod();
try {
URI base = new URI(url, false);
method.setURI(new URI(base, "HttpBatchSendSM", false));
method.setRequestBody (new NameValuePair[] {
new NameValuePair("account", account),
new NameValuePair("password", pswd),
new NameValuePair("mobiles", mobile),
new NameValuePair("msg", msg),
new NameValuePair("needstatus", String.valueOf(needstatus)),
new NameValuePair("product", product)
});
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8"); 
int 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);
}
return URLDecoder.decode(baos.toString(), "UTF-8");
} else {
Date date=new Date();
String time=(1900+date.getYear())+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText()+":出错内容"+msg+":出错时间"+time);
}
}finally {
method.releaseConnection();
}
}

猜你喜欢

转载自szqz123.iteye.com/blog/2280683