最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼

 更新時(shí)間:2019年07月13日 08:37:03   作者:思想聚焦  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近用學(xué)習(xí)了一下調(diào)用第三方接口發(fā)送短信驗(yàn)證碼的程序,希望能夠幫助到大家。

1.首先下圖為項(xiàng)目的目錄結(jié)構(gòu),需要帶入三個(gè)包:

commons-httpclient-3.1.jar

commons-logging-1.0.4.jar

codec-1.3.jar

2.其次要?jiǎng)?chuàng)建模擬POST、GET請(qǐng)求的工具類:

package com.demo.util;
 
 
import java.io.IOException;
import java.util.Map;
 
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
 
 
public class HttpRequestUtil {
 /**
 * HttpClient 模擬POST請(qǐng)求
 * @param url
 * @param params
 * @return
 */
 public static String postRequest(String url, Map<String, String> params) {
  //構(gòu)造HttpClient的實(shí)例
  HttpClient httpClient = new HttpClient();
 
 
  //創(chuàng)建POST方法的實(shí)例
  PostMethod postMethod = new PostMethod(url);
 
 
  //設(shè)置請(qǐng)求頭信息
  postMethod.setRequestHeader("Connection", "close");
 
 
  //添加參數(shù)
  for (Map.Entry<String, String> entry : params.entrySet()) {
   postMethod.addParameter(entry.getKey(), entry.getValue());
  }
 
 
  //使用系統(tǒng)提供的默認(rèn)的恢復(fù)策略,設(shè)置請(qǐng)求重試處理,用的是默認(rèn)的重試處理:請(qǐng)求三次
  httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
 
 
  //接收處理結(jié)果
  String result = null;
  try {
   //執(zhí)行Http Post請(qǐng)求
   httpClient.executeMethod(postMethod);
 
 
   //返回處理結(jié)果
   result = postMethod.getResponseBodyAsString();
  } catch (HttpException e) {
   // 發(fā)生致命的異常,可能是協(xié)議不對(duì)或者返回的內(nèi)容有問(wèn)題
   System.out.println("請(qǐng)檢查輸入的URL!");
   e.printStackTrace();
  } catch (IOException e) {
   // 發(fā)生網(wǎng)絡(luò)異常
   System.out.println("發(fā)生網(wǎng)絡(luò)異常!");
   e.printStackTrace();
  } finally {
   //釋放鏈接
   postMethod.releaseConnection();
 
 
   //關(guān)閉HttpClient實(shí)例
   if (httpClient != null) {
    ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
    httpClient = null;
   }
  }
  return result;
 }
 
 
 /**
  * HttpClient 模擬GET請(qǐng)求
  * 方法說(shuō)明
  * @Discription:擴(kuò)展說(shuō)明
  * @param url
  * @param params
  * @return String
  */
 public static String getRequest(String url, Map<String, String> params) {
  //構(gòu)造HttpClient實(shí)例
  HttpClient client = new HttpClient();
 
 
  //拼接參數(shù)
  String paramStr = "";
  for (String key : params.keySet()) {
   paramStr = paramStr + "&" + key + "=" + params.get(key);
  }
  paramStr = paramStr.substring(1);
 
 
  //創(chuàng)建GET方法的實(shí)例
  GetMethod method = new GetMethod(url + "?" + paramStr);
 
 
  //接收返回結(jié)果
  String result = null;
  try {
   //執(zhí)行HTTP GET方法請(qǐng)求
   client.executeMethod(method);
 
 
   //返回處理結(jié)果
   result = method.getResponseBodyAsString();
  } catch (HttpException e) {
   // 發(fā)生致命的異常,可能是協(xié)議不對(duì)或者返回的內(nèi)容有問(wèn)題
   System.out.println("請(qǐng)檢查輸入的URL!");
   e.printStackTrace();
  } catch (IOException e) {
   // 發(fā)生網(wǎng)絡(luò)異常
   System.out.println("發(fā)生網(wǎng)絡(luò)異常!");
   e.printStackTrace();
  } finally {
   //釋放鏈接
   method.releaseConnection();
 
 
   //關(guān)閉HttpClient實(shí)例
   if (client != null) {
    ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
    client = null;
   }
  }
  return result;
 }
}

3.創(chuàng)建發(fā)送短信的方法,其中要通過(guò)短信內(nèi)容要進(jìn)行設(shè)置編碼集為utf-8,調(diào)用第三方接口傳參要按照第三方文檔規(guī)范:

package com.demo.util;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
 
 
public class SendMsgUtil {
 /**
  * 發(fā)送短信消息
  * 方法說(shuō)明
  * @Discription:擴(kuò)展說(shuō)明
  * @param phones
  * @param content
  * @return
  * @return String
  */
 @SuppressWarnings("deprecation")
 public static String sendMsg(String phones,String content) {
 try {
 content = java.net.URLEncoder.encode(content,"utf-8");
 System.out.println(content);
 } catch (UnsupportedEncodingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
  //短信接口URL提交地址
  String url = "https://api.dingdongcloud.com/v1/sms/sendyzm?apikey=b46c4961aa875f626b7924aace0d53f7&mobile="+phones+"&content="+content;
 
  Map<String, String> params = new HashMap<String, String>();
 
  params.put("zh", "賬號(hào)");
  params.put("mm", "密碼");
  params.put("dxlbid", "短信類別編號(hào)");
  params.put("extno", "擴(kuò)展編號(hào)");
 
  //手機(jī)號(hào)碼,多個(gè)號(hào)碼使用英文逗號(hào)進(jìn)行分割
  params.put("hm", phones);
  //將短信內(nèi)容進(jìn)行URLEncoder編碼
  params.put("nr", URLEncoder.encode(content));
 
  return HttpRequestUtil.getRequest(url, params);
 }
 
 /**
  * 隨機(jī)生成6位隨機(jī)驗(yàn)證碼
  * 方法說(shuō)明
  * @Discription:擴(kuò)展說(shuō)明
  * @return
  * @return String
  */
 public static String createRandomVcode(){
  //驗(yàn)證碼
  String vcode = "";
  for (int i = 0; i < 6; i++) {
   vcode = vcode + (int)(Math.random() * 9);
  }
  return vcode;
 }
 /**
  * 測(cè)試
  * 方法說(shuō)明
  * @Discription:擴(kuò)展說(shuō)明
  * @param args
  * @return void
  */
 public static void main(String[] args) {
//  System.out.println(SendMsgUtil.createRandomVcode());
//  System.out.println("&ecb=12".substring(1));
  System.out.println(sendMsg("18201150549", "【簽名】尊敬的用戶,您的驗(yàn)證碼為" + SendMsgUtil.createRandomVcode() + ",請(qǐng)?jiān)?0分鐘內(nèi)輸入。請(qǐng)勿告訴其他人!"));
 }
}

4.調(diào)用main方法可以進(jìn)行測(cè)試,如果控臺(tái)太輸出返回狀態(tài)值不是200,可以參考第三方發(fā)短信的文檔返回參數(shù)說(shuō)明。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

柘城县| 兴山县| 彰武县| 比如县| 昌宁县| 云南省| 千阳县| 肃宁县| 溧阳市| 湖州市| 额尔古纳市| 无为县| 新沂市| 赤城县| 阿克陶县| 文水县| 天津市| 隆林| 博湖县| 威远县| 夏邑县| 香港 | 建水县| 眉山市| 鄂托克前旗| 尉犁县| 湖北省| 大同县| 姚安县| 晋州市| 望江县| 边坝县| 页游| 垫江县| 永康市| 大同市| 林周县| 应用必备| 鄱阳县| 曲松县| 静海县|