package util;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
public class SendSmsUtil {
String smsurl = "http://115.239.133.245:8080/YL_sms/http/sendSMS";
/**
* 发送短信
*
* @param url
* @param phone
* @param content
* @param netType
* @param gwTyep
* @return
*/
public String sendSms(String phone, String content) {
PostMethod postMethod = null;
HttpClient httpClient = null;
String returnValue = "";
String netType = "1";
String gwType = "8";
if(isChinaTeleCom(phone)){
netType="0";
gwType="32";
}
try {
postMethod = new PostMethod(smsurl);// 通知接口
postMethod.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
postMethod.setParameter("USERID", "file");
postMethod.setParameter("PASSWORD", "123456");
postMethod.setParameter("PHONENO", phone);
postMethod.setParameter("SMSTEXT", content);
postMethod.setParameter("NETTYPE", netType);
postMethod.setParameter("ECPNO", phone);
postMethod.setParameter("SENDERECP", "");
postMethod.setParameter("SENDERNAME", "");
postMethod.setParameter("PR", "5");
postMethod.setParameter("FLAG", "1");
postMethod.setParameter("GWTYPE", gwType);
httpClient = new HttpClient();
// 设置超时时间10秒
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(10000);
// 设置返回超时10秒
httpClient.getHttpConnectionManager().getParams().setSoTimeout(
10000);
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
log.info("渠道处理 failed: " + postMethod.getStatusLine());
returnValue = "fail";
} else {
log.info("调用渠道处理接口成功!");
returnValue = "success";
}
} catch (Exception e) {
log.error("发送短信异常:", e);
returnValue = "fail";
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
postMethod = null;
}
((SimpleHttpConnectionManager) httpClient
.getHttpConnectionManager()).shutdown();
}
return returnValue;
}
/**
* 判断是否电信号码
*
* @param mobile
* @return
*/
public boolean isChinaTeleCom(String mobile) {
boolean result = false;
try {
if (mobile.trim().startsWith("133")
|| mobile.trim().startsWith("153")
|| mobile.trim().startsWith("189")
|| mobile.trim().startsWith("180")) {
result = true;
}
} catch (Exception e) {
result = false;
log.error("判断是否电信号码异常:", e);
}
return result;
}
}
原文:http://blog.csdn.net/u011328015/article/details/43982979