A function of sending SMS verification codes uses Xinyitong's SMS platform interface, and then uses HttpClient to simulate POST requests or GET requests in Java (see the SMS platform requirements, which are generally POST requests), and calls the interface provided by the SMS platform (just follow the interface specifications of the SMS platform). Depend on the code:
When using HttpClient, you need to introduce it in the project:
commons-httpclient-3.1.jar
This jar package,
Project structure:
1. Create an Http simulation request tool class, and then write a POST method or GET method
/** * File Description* @Description: Extended Description* @Copyright: 2015 dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util;import java.io.IOException;import java.util.Map;import org.apache.commons.httpclient.HttpClient;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;/** * @Author: feizi * @Date: April 17, 2015 at 9:26:34 am * @ModifyUser: feizi * @ModifyDate: April 17, 2015 at 9:26:34 am * @Version:V6.0 */public class HttpRequestUtil { /** * HttpClient simulated POST request* Method description* @Discription:Extended description* @param url * @param params * @return String * @Author: feizi * @Date: April 17, 2015 at 7:15:59 pm * @ModifyUser: feizi * @ModifyDate: April 17, 2015 at 7:15:59 pm */ public static String postRequest(String url, Map<String, String> params) { //Construct an instance of HttpClient HttpClient httpClient httpClient = new HttpClient(); //Create an instance of POST method PostMethod postMethod = new PostMethod(url); //Set request header information postMethod.setRequestHeader("Connection", "close"); //Add parameters for (Map.Entry<String, String> entry : params.entrySet()) { postMethod.addParameter(entry.getKey(), entry.getValue()); } //Use the default recovery strategy provided by the system to set the request retry processing, using the default retry processing: request three times httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false); //Receive processing results String result = null; try { //Execute Http Post request httpClient.executeMethod(postMethod); //Return processing result result = postMethod.getResponseBodyAsString(); } catch (HttpException e) { // A fatal exception occurred, which may be due to a mismatched protocol or a problem with the returned content System.out.println("Please check the entered URL!"); e.printStackTrace(); } catch (IOException e) { // A network exception occurred System.out.println("Network exception occurred!"); e.printStackTrace(); } finally { //Release the link postMethod.releaseConnection(); //Close the HttpClient instance if (httpClient != null) { ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown(); httpClient = null; } } return result; } /** * HttpClient simulates GET request* Method description* @Discription:Extension description* @param url * @param params * @return String * @Author: feizi * @Date: April 17, 2015 at 7:15:28 pm * @ModifyUser: feizi * @ModifyDate: April 17, 2015 at 7:15:28 pm */ public static String getRequest(String url, Map<String, String> params) { //Construct HttpClient instance HttpClient client = new HttpClient(); //Split parameters String paramStr = ""; for (String key : params.keySet()) { paramStr = paramStr + "&" + key + "=" + params.get(key); } paramStr = paramStr.substring(1); //Create an instance of the GET method GetMethod method = new GetMethod(url + "?" + paramStr); //Receive the return result String result = null; try { //Execute the HTTP GET method request client.executeMethod(method); //Return the processing result result = method.getResponseBodyAsString(); } catch (HttpException e) { // A fatal exception occurred, which may be that the protocol is incorrect or there is something wrong with the returned content System.out.println("Please check the entered URL!"); e.printStackTrace(); } catch (IOException e) { // Network exception occurred System.out.println("Network exception occurred!"); e.printStackTrace(); } finally { //Release link method.releaseConnection(); //Close the HttpClient instance if (client != null) { ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown(); client = null; } } return result; }}2. Create a class, generate a verification code, and then pass the corresponding parameters (different SMS platform interfaces will have different parameter requirements. This general SMS platform provides interface documents. Just look at the document and then follow the requirements)
/** * File Description* @Description: Extended Description* @Copyright: 2015 dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util;import java.net.URLEncoder;import java.util.HashMap;import java.util.Map;/** * @Author: feizi * @Date: April 17, 2015 at 9:24:48 am * @ModifyUser: feizi * @ModifyDate: April 17, 2015 at 9:24:48 am * @Version:V6.0 */public class SendMsgUtil { /** * Send SMS message* Method description* @Discription:Extended description* @param phones * @param content * @return * @return String * @Author: feizi * @Date: April 17, 2015 at 7:18:08 pm * @ModifyUser: feizi * @ModifyDate: April 17, 2015 at 7:18:08 pm */ @SuppressWarnings("deprecation") public static String sendMsg(String phones,String content){ //SMS interface URL submission address String url = "SMS interface URL submission address"; Map<String, String> params = new HashMap<String, String>(); params.put("zh", "User Account"); params.put("mm", "User Password"); params.put("dxlbid", "SMS category number"); params.put("extno", "Extended number"); //Mobile phone number, multiple numbers are divided using English commas params.put("hm", phones); //User encoded SMS content params.put("nr", URLEncoder.encode(content)); return HttpRequestUtil.getRequest(url, params); } /** * Randomly generate 6-bit random verification code* Method description* @Discription:Extended description* @return * @return String * @Author: feizi * @Date: April 17, 2015 at 7:19:02 pm * @ModifyUser: feizi * @ModifyDate: April 17, 2015 at 7:19:02 pm */ public static String createRandomVcode(){ //Vertification code String vcode = ""; for (int i = 0; i < 6; i++) { vcode = vcode + (int)(Math.random() * 9); } return vcode; } /** * Test* Method Description* @Discription:Extended Description* @param args * @return void * @Author: feizi * @Date: April 17, 2015 at 7:26:36 pm * @ModifyUser: feizi * @ModifyDate: April 17, 2015 at 7:26:36 pm */ public static void main(String[] args) {// System.out.println(SendMsgUtil.createRandomVcode());// System.out.println("&ecb=12".substring(1)); System.out.println(sendMsg("18123456789,15123456789", "Dear user, your verification code is " + SendMsgUtil.createRandomVcode() + ", valid for 60 seconds. If you have any questions, please ask 400-069-2886 (customer service phone number) [XXX Center]")); }}Then execute it. Generally, if the parameters are passed correctly, and if the interface document is operated according to the specifications, the sending will be successful, and the mobile phone will receive the verification code. The problem that may arise is: the content of the sent text message may have Chinese garbled code, and the sending will be unsuccessful. The corresponding encoding will be performed according to the requirements of the text message platform. It is usually UTF-8 encoding.
Complete code: sendmassage
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.