最近在作CRM的項目,先接觸到的是發送短信。我是經過SMS平臺來發送短信的,本人仍是菜鳥,若是有說的不對的還望你們給予指正,先謝謝了。java
1.先到短信平臺去註冊用戶web
2.註冊成功後,到接口API下找到UID和KEY,能夠進行修改。apache
GBK編碼發送接口地址:
http://gbk.sms.webchinese.cn/?Uid=本站用戶名&Key=接口安全密碼&smsMob=手機號碼&smsText=短信內容
UTF-8編碼發送接口地址:
http://utf8.sms.webchinese.cn/?Uid=本站用戶名&Key=接口安全密碼&smsMob=手機號碼&smsText=短信內容
獲取短信數量接口地址(UTF8):
http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid=本站用戶名&Key=接口安全密碼
獲取短信數量接口地址(GBK):
http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid=本站用戶名&Key=接口安全密碼 api
短信調用後會有相應的返回值,能夠到SMS官網去查看返回值對應的意思。安全
3.來看看我寫的發送短信的工具類app
package com.*.*.bsm.utils;ide
import com.*.*.sys.exception.SysException;工具
import org.apache.commons.httpclient.Header;post
import org.apache.commons.httpclient.HttpClient;this
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 14-11-26
* Time: 下午2:38
* To change this template use File | Settings | File Templates.
*/
public class SendMsg {
public static String sendMsg(String toUser,String content) throws IOException{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在頭文件中設置轉碼
NameValuePair[] data ={ new NameValuePair("Uid", "你的UID"),new NameValuePair("Key", "你的KEY"),new NameValuePair("smsMob",toUser),new NameValuePair("smsText",content)};
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
for(Header h : headers)
{
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println(result); //打印返回消息狀態
post.releaseConnection();
return result;
}
}