建一個utlls
package com.itjh.util;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import java.util.Random;
public class SendMessageUtil {
private static final StringSMS_Url ="http://sms.webchinese.cn/web_api/";
/**
? ? * @param Uid SMS用戶id? : lvfang123
? ? * @param Key 接口秘鑰:SMS登錄可查(非登錄密碼)
? ? * @param sendPhoneNum 短信發(fā)送目標號碼
? ? * @param desc 短信內(nèi)容
? ? * @return Integer(1:成功碼狈谊,其他失敗男应,具體參見注釋)
*/
? ? public static Integersend(String Uid,String Key,String sendPhoneNum,String desc){
HttpClient client =new HttpClient();
PostMethod post =new PostMethod(SMS_Url);
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");// 在頭文件中設置轉(zhuǎn)碼
? ? ? ? //設置參數(shù)
? ? ? ? NameValuePair[]data = {
new NameValuePair("Uid", Uid),
new NameValuePair("Key", Key),//秘鑰
? ? ? ? ? ? ? ? new NameValuePair("smsMob", sendPhoneNum),
new NameValuePair("smsText", desc)
};
post.setRequestBody(data);
try {
client.executeMethod(post);
}catch (Exception e) {? e.printStackTrace();? }
Header[]headers =post.getResponseHeaders();
int statusCode =post.getStatusCode();
System.out.println("statusCode:" +statusCode);
for (Header h :headers) {
System.out.println(h.toString());
}
String result ="";
try {
result =new String(post.getResponseBodyAsString().getBytes("gbk"));
}catch (Exception e) {? e.printStackTrace();? }
post.releaseConnection();
return Integer.parseInt(result);
}
/**
? ? *? -1? 沒有該用戶賬戶
? ? -2 接口密鑰不正確 [查看密鑰]不是賬戶登陸密碼
? ? -21? ? MD5接口密鑰加密不正確
? ? -3 短信數(shù)量不足
? ? -11? ? 該用戶被禁用
? ? -14? ? 短信內(nèi)容出現(xiàn)非法字符
? ? -4 手機號格式不正確
? ? -41? ? 手機號碼為空
? ? -42? ? 短信內(nèi)容為空
? ? -51? ? 短信簽名格式不正確接口簽名格式為:【簽名內(nèi)容】
? ? -6 IP限制
? ? 大于0? ? 短信發(fā)送數(shù)量
以上作為補充
? ? */
? ? public static StringgetMessage(Integer code){
Stringmessage;
if(code >0 ) {
message ="SMS-f發(fā)送成功渐夸!短信量還有" + code +"條";
}else if(code == -1){
message ="SMS-沒有該用戶賬戶";
}else if(code == -2){
message ="SMS-接口密鑰不正確";
}else if(code == -21){
message ="SMS-MD5接口密鑰加密不正確";
}else if(code == -3){
message ="SMS-短信數(shù)量不足";
}else if(code == -11){
message ="SMS-該用戶被禁用";
}else if(code == -14){
message ="SMS-短信內(nèi)容出現(xiàn)非法字符";
}else if(code == -4){
message ="SMS-手機號格式不正確";
}else if(code == -41){
message ="SMS-手機號碼為空";
}else if(code == -42){
message ="SMS-短信內(nèi)容為空";
}else if(code == -51){
message ="SMS-短信簽名格式不正確接口簽名格式為:【簽名內(nèi)容】";
}else if(code == -6){
message ="SMS-IP限制";
}else{
message ="其他錯誤";
}
return message;
}
/**
? ? * 隨機生成6位驗證碼
? ? * @return
*/
? ? public static StringgetRandomCode(Integer code){
Randomrandom =new Random();
StringBufferresult=new StringBuffer();
for (int i=0;i
result.append(random.nextInt(10));
}
return result.toString();
}
public static StringgetRandomCode(){
Randomrandom =new Random();
String result="";
for (int i=0;i<6;i++){
result+=random.nextInt(10);
}
return result;
}
public void testSendMessage(){
//? ? ? ? SendMessageUtil.send("SMS賬戶","接口秘鑰","目標號碼","發(fā)送內(nèi)容");
//第一個名稱,密鑰鱼鸠,手機號
? ? ? ? SendMessageUtil.send("jimingyu","d41d8cd98f00b204e980","15226921418","驗證碼:"+getRandomCode(6));
IntegerresultCode =1;
System.out.println(SendMessageUtil.getMessage(resultCode));
}
}
配置文件pom.xml
commons-logging
commons-logging
1.1.1
commons-codec
commons-codec
1.4
commons-httpclient
commons-httpclient
3.0.1
獲取發(fā)送短信密鑰網(wǎng)址
http://sms.webchinese.com.cn/User/?action=key