根据用户手机号,发送短信 public static String sendMessage(String phone, String message) throws Exception { HttpClient client = new HttpClient(); PostMethod post = new PostMethod("http://gbk.api.smschinese.cn"); //在头文件中设
public static String sendMessage(String phone, String message) throws Exception {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");
//在头文件中设置转码
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");
NameValuePair[] data = {
// Uid和Key需要在网站进行注册
new NameValuePair("Uid", "*******"),
new NameValuePair("Key", "*****************"),
new NameValuePair("smsMob", phone),
new NameValuePair("smsText", message)
};
post.setRequestBody(data);
client.executeMethod(post);
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
post.releaseConnection();
switch (Integer.parseInt(result)) {
case -1:
throw new Exception("没有该用户账户");
case -2:
throw new Exception("密钥不正确");
case -3:
throw new Exception("短信数量不足");
case -11:
throw new Exception("该用户被禁用");
case -14:
throw new Exception("短信内容出现非法字符");
case -41:
throw new Exception("手机号码为空");
case -42:
throw new Exception("短信内容为空");
default:
result = "发送成功,发送数量:" + result;
break;
}
return result;
}
