1. 程式人生 > >java呼叫傳送簡訊API

java呼叫傳送簡訊API

需要用到的jar包

這裡寫圖片描述

工具類和測試類

工具類:


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; } /** * @author wangzs * @Description 傳送 post請求 * @date
2017-11-6 * @time 下午4:
36:34 * @param httpUrl 地址 * @return */ public String sendHttpPost(String httpUrl) { HttpPost httpPost = new HttpPost(httpUrl);// 建立httpPost return sendHttpPost(httpPost,"utf-8"); } /** * @author wangzs * @Description 傳送 post請求
* @date 2017-11-6 * @time 下午4:36:58 * @param httpUrl 地址 * @param maps 引數 * @param type 字元編碼格式 * @return */ 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); } /** * @author wangzs * @Description 傳送Post請求 * @date 2017-11-6 * @time 下午4:38:18 * @param httpPost * @param reponseType * @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; } /** * @author wangzs * @Description 傳送 get請求 * @date 2017-11-6 * @time 下午4:38:39 * @param httpUrl * @return */ public String sendHttpGet(String httpUrl) { HttpGet httpGet = new HttpGet(httpUrl);// 建立get請求 return sendHttpGet(httpGet); } /** * @author wangzs * @Description 傳送 get請求Https * @date 2017-11-6 * @time 下午4:38:52 * @param httpUrl * @return */ public String sendHttpsGet(String httpUrl) { HttpGet httpGet = new HttpGet(httpUrl);// 建立get請求 return sendHttpsGet(httpGet); } /** * @author wangzs * @Description TODO(傳送utf8) * @date 2017-11-6 * @time 下午4:39:16 * @param Uid * @param Key * @param content * @param mobiles * @return */ @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); } /** * @author wangzs * @Description TODO(傳送utf8) * @date 2017-11-6 * @time 下午4:39:58 * @param Uid * @param Key * @param content * @param mobiles * @return */ @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); } /** * @author wangzs * @Description 傳送Get請求 * @date 2017-11-6 * @time 下午4:40:28 * @param httpGet * @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; } /** * @author wangzs * @Description 傳送Get請求Https * @date 2017-11-6 * @time 下午4:40:44 * @param httpGet * @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; } /** * @author wangzs * @Description TODO(返回異常原因) * @date 2017-11-6 * @time 下午4:40:57 * @param errorCode * @return */ 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; } } }

測試類:


public class test {


    //使用者名稱和祕鑰都需要到中國網建提供的SMS簡訊平臺上檢視

    //使用者名稱
    private static String Uid = "WangZongShi";

    //介面安全祕鑰
    private static String Key = "3d99cc5wqeffed58";

    //手機號碼,多個號碼如13800000000,13800000001,13800000002
    private static String smsMob = "18888888888";

    //簡訊內容
    private static String smsText = "驗證碼:8888";


    public static void main(String[] args) {

        HttpClientUtil client = HttpClientUtil.getInstance();

        //UTF傳送
        int result = client.sendMsgUtf8(Uid, Key, smsText, smsMob);
        if(result>0){
            System.out.println("UTF8成功傳送條數=="+result);
        }else{
            System.out.println(client.getErrorMsg(result));
        }
    }
}

會出現的問題:

    請各位大神發現並提出。
    1.
    2.
    3.
    。
    。
    。

謝謝觀看~