下面是公司项目现成的代码:
需要的jar包:
commons-httpclient
commons-lang
commons-logging
commons-codec
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
public class SMSTest {
private static final String KEY_CELL = "Cell";
private static final String KEY_CONTENT = "Content";
private static final String KEY_MOBILE = "Mobile";
private static final String KEY_PWD = "Pwd";
private static final String KEY_USERNAME = "CorpID";
private static final String KEY_SEND_TIME = "SendTime";
private static final Pattern RESPONSE_FAILED_PATTERN = Pattern.compile("^-\\d{1,}$");
public void send(String mobile, String content) throws Exception {
NameValuePair[] parametersBody = contructParams4Send(content, mobile);
sendRequestAndGetResponse(parametersBody, "sendUrl"); //sendUrl短信中心地址
}
private NameValuePair[] contructParams4Send(String text, String mobiles){
NameValuePair paramUsername = new NameValuePair(KEY_USERNAME, "*******");
NameValuePair paramPassword = new NameValuePair(KEY_PWD, "**********");
NameValuePair mobile = new NameValuePair(KEY_MOBILE, mobiles);
NameValuePair content = new NameValuePair(KEY_CONTENT, text);
NameValuePair cell = new NameValuePair(KEY_CELL, "");
NameValuePair sendTime = new NameValuePair(KEY_SEND_TIME, "");
NameValuePair[] parametersBody = new NameValuePair[] { paramUsername, paramPassword, mobile, content, cell,sendTime };
return parametersBody;
}
private String sendRequestAndGetResponse(NameValuePair[] parametersBody, String url) throws Exception {
HttpClient httpClient = new HttpClient();
httpClient .getParams().setParameter("http.protocol.content-charset", "gb2312");
PostMethod post = new PostMethod(url);
try {
post.setRequestBody(parametersBody);
httpClient.executeMethod(post);
String response = post.getResponseBodyAsString();
if (!isFailed(response)) {
System.out.println("send message success,response code is<" + response + ">");
return response;
}else{
System.out.println("send message failed,response code is<" + response + ">," + SMSResponseCode.getMsg(response));
throw new Exception("发送失败!");
}
} catch (Exception e) {
System.out.println("send message failed." + e.getMessage());
throw new Exception(e);
} finally {
if (post != null) {
post.releaseConnection();
}
}
}
private boolean isFailed(String response) {
return RESPONSE_FAILED_PATTERN.matcher(StringUtils.trim(response)).matches();
}
public static void main(String[] args) {
try {
new SMSTest().send("************", "哇哈哈哈哈");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public enum SMSResponseCode {
SUCCESS("1", "发送成功"), NOT_REGIST("-1", "账号未注册"), OTHER_ERROR("-2", "其他错误"),
INVALID_USERNAME_PWD("-3", "帐号或密码错误"), MAX_MOBILES("-4", "一次提交信息不能超过600个手机号码"),
BALANCE_NOT_ENOUGH("-5", "余额不足,请先充值"), INVALID_SEND_TIME("-6", "定时发送时间不是有效的时间格式"),
INVALID_CONTENT_LENGTH("-8", "发送内容需在3到250字之间") , EMPYT_MOBILE("-9", "发送号码为空");
private String responseCode;
private String msg;
public String getResponseCode() {
return responseCode;
}
public String getMsg() {
return msg;
}
private SMSResponseCode(String responseCode, String msg) {
this.responseCode = responseCode;
this.msg = msg;
}
public static String getMsg(String responseCode) {
for (SMSResponseCode code : SMSResponseCode.values()) {
if (code.getResponseCode().equals(responseCode)) {
return code.getMsg();
}
}
return "未知错误!";
}
}
本文介绍了一种使用Java实现的短信发送方法,通过调用特定的API接口完成短信发送任务。该实现利用了Apache Commons库中的多个组件,如HTTP客户端、语言工具等。
5971

被折叠的 条评论
为什么被折叠?



