中國網建短信平臺接口
http://www.smschinese.cn/api.shtml
步驟:
1. 短信通平臺用戶註冊: http://www.smschinese.cn/reg.shtml
2.用戶登錄:用戶登錄之後,可以發送5條免費短信。
3.用戶信息修改:需要修改密碼和短信簽名(短信開頭會加上【短信簽名內容】,一般爲公司名或產品名的簡稱)
4.點擊短信API接口
點擊JAVA程序示例
下載演示程序
解壓後,打開其中一個目錄,如msgProGBK,目錄結構如下
5.測試
5.1 用eclipse打開
用eclipse打開解壓後的目錄,注意需要給.java文件添加包,例如:
demo裏代碼已經寫好,貼出來,方便查看
HttpClientUtil.java
package msgProGBK;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
private RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(15000)
.setConnectTimeout(15000)
.setConnectionRequestTimeout(15000)
.build();
private static HttpClientUtil instance = null;
private HttpClientUtil(){}
public static HttpClientUtil getInstance(){
if (instance == null) {
instance = new HttpClientUtil();
}
return instance;
}
/**
* 發送 post請求
* @param httpUrl 地址
*/
public String sendHttpPost(String httpUrl) {
HttpPost httpPost = new HttpPost(httpUrl);// 創建httpPost
return sendHttpPost(httpPost,"utf-8");
}
/**
* 發送 post請求
* @param httpUrl 地址
* @param maps 參數
* @param type 字符編碼格式
*/
public String sendHttpPost(String httpUrl, Map<String, String> maps,String type) {
HttpPost httpPost = new HttpPost(httpUrl);// 創建httpPost
// 創建參數隊列
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (String key : maps.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, type));
} catch (Exception e) {
e.printStackTrace();
}
return sendHttpPost(httpPost,type);
}
/**
* 發送Post請求
* @param httpPost
* @return
*/
private String sendHttpPost(HttpPost httpPost,String reponseType) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
// 創建默認的httpClient實例.
httpClient = HttpClients.createDefault();
httpPost.setConfig(requestConfig);
// 執行請求
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, reponseType);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 關閉連接,釋放資源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
/**
* 發送 get請求
* @param httpUrl
*/
public String sendHttpGet(String httpUrl) {
HttpGet httpGet = new HttpGet(httpUrl);// 創建get請求
return sendHttpGet(httpGet);
}
/**
* 發送 get請求Https
* @param httpUrl
*/
public String sendHttpsGet(String httpUrl) {
HttpGet httpGet = new HttpGet(httpUrl);// 創建get請求
return sendHttpsGet(httpGet);
}
/**
* @Title: sendMsgUtf8
* @Description: TODO(發送utf8)
* @param: @param Uid
* @param: @param Key
* @param: @param content
* @param: @param mobiles
* @param: @return
* @return: int
* @throws
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public int sendMsgUtf8(String Uid,String Key,String content,String mobiles){
Map maps = new HashMap();
maps.put("Uid", Uid);
maps.put("Key", Key);
maps.put("smsMob", mobiles);
maps.put("smsText", content);
String result = sendHttpPost("http://utf8.sms.webchinese.cn", maps, "utf-8");
return Integer.parseInt(result);
}
/**
* @Title: sendMsgUtf8
* @Description: TODO(發送utf8)
* @param: @param Uid
* @param: @param Key
* @param: @param content
* @param: @param mobiles
* @param: @return
* @throws
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public int sendMsgGbk(String Uid,String Key,String content,String mobiles){
Map maps = new HashMap();
maps.put("Uid", Uid);
maps.put("Key", Key);
maps.put("smsMob", mobiles);
maps.put("smsText", content);
String result = sendHttpPost("http://gbk.sms.webchinese.cn", maps, "gbk");
return Integer.parseInt(result);
}
/**
* 發送Get請求
* @param httpPost
* @return
*/
private String sendHttpGet(HttpGet httpGet) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
// 創建默認的httpClient實例.
httpClient = HttpClients.createDefault();
httpGet.setConfig(requestConfig);
// 執行請求
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 關閉連接,釋放資源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
/**
* 發送Get請求Https
* @param httpPost
* @return
*/
private String sendHttpsGet(HttpGet httpGet) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
// 創建默認的httpClient實例.
PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
httpGet.setConfig(requestConfig);
// 執行請求
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 關閉連接,釋放資源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
/**
* @Title: getErrorMsg
* @Description: TODO(返回異常原因)
* @param: @param errorCode
*/
public String getErrorMsg(int errorCode){
if(errorCode==-1){
return "沒有該用戶賬戶";
}else if(errorCode==-2){
return "接口密鑰不正確";
}else if(errorCode==-3){
return "短信數量不足";
}else if(errorCode==-4){
return "手機號格式不正確";
}else if(errorCode==-21){
return "MD5接口密鑰加密不正確";
}else if(errorCode==-11){
return "該用戶被禁用";
}else if(errorCode==-14){
return "短信內容出現非法字符";
}else if(errorCode==-41){
return "手機號碼爲空";
}else if(errorCode==-42){
return "短信內容爲空";
}else if(errorCode==-51){
return "短信簽名格式不正確";
}else if(errorCode==-6){
return "IP限制";
}else{
return "未知錯誤碼:"+errorCode;
}
}
}
test.java
package msgProGBK;
import java.util.HashMap;
import java.util.Map;
public class test {
//用戶名
private static String Uid = "填入網建網的登錄用戶名";
//接口安全祕鑰
private static String Key = "填入密鑰";
//手機號碼,多個號碼如13800000000,13800000001,13800000002
private static String smsMob = "填寫要發送到的手機號";
//短信內容
private static String smsText = "驗證碼:測試一下8888";
public static void main(String[] args) {
HttpClientUtil client = HttpClientUtil.getInstance();
//GBK發送
int resultGbk = client.sendMsgGbk(Uid, Key, smsText, smsMob );
if(resultGbk>0){
System.out.println("GBK成功發送條數=="+resultGbk);
}else{
System.out.println(client.getErrorMsg(resultGbk));
}
}
}
注意修改如下信息:
Uid = "填入網建網的登錄用戶名";
Key = "填入密鑰"; 參考:密鑰的獲取方式
smsMob = "填寫要發送到的手機號";
smsText = "驗證碼:測試一下8888";
密鑰的獲取方式:點擊修改短息密鑰,複製短信密鑰,填入Key中
運行test.java, 手機接收到的短信如下:
5.2 用IDEA maven方式
用maven方式更容易追蹤源碼,方便理解代碼。
新建maven工程,選maven-archetype-quickstart
修改pom.xml,添加依賴
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.12</version>
</dependency>
新建兩個類
HttpClientUtil.java和test.java,代碼和上面一樣。
運行測試。
完成!enjoy it!