java servlet mobile app access interface of SMS verification

  • 2020-05-24 05:40:34
  • OfStack

Today to find a few short message platform, actually want to use a sharesdk, use it http api message function, not only low prices, and with a minimum of 100 RMB top-up but too strict review corresponding APP must also integrated their message function, but also want to upload the audit has to more than 20 days, I also just want to find a SMS platform under test, so even if it. Then in baidu casually in good 1 SMS platform www.wasun.cn, temporarily feel it is good, at least it gave the test account to accept SMS speed did not exceed 5 seconds, I looked at 1 is 3 seconds or even faster. Next, I will talk about the method to call the SMS interface, and the problems encountered in the use.

1. Request methods in httprequest mode

He gave DOMO had really good encapsulation method, is to use httpClient request, in before. NET used in this class, and. net also directly HttpWebRequest this class, I look at the code in its function should be encapsulated in the java to URLConnection inside the class, because of the time, packaging method I didn't study is from the Internet to find, but should be. The HttpWebRequest net is a meaning. The following code is pasted, including the code of httpClient of DEMO generation.


package Helper;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpRequest {
  /**
   *  To specify the URL send GET Method request 
   * 
   * @param url
   *       requesting URL
   * @param param
   *       Request parameter, request parameter should be  name1=value1&name2=value2  In the form. 
   * @return URL  The response result of the remote resource represented 
   */
  public static String sendGet(String url, String param) {
    String result = "";
    BufferedReader in = null;
    
    try {
      String urlNameString = url + "?" + param;
      URL realUrl = new URL(urlNameString);
      //  Open and URL Connection between 
      URLConnection connection = realUrl.openConnection();
      //  Sets the generic request properties 
      connection.setRequestProperty("accept", "*/*");
      connection.setRequestProperty("connection", "Keep-Alive");
      connection.setRequestProperty("user-agent",
          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
      //  Establish the actual connection 
      connection.connect();
      //  Gets all the response header fields 
      Map<String, List<String>> map = connection.getHeaderFields();
      //  Traverse all the response header fields 
      for (String key : map.keySet()) {
        System.out.println(key + "--->" + map.get(key));
      }
      //  define  BufferedReader Input stream to read URL The response of the 
      in = new BufferedReader(new InputStreamReader(
          connection.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line;
      }
    } catch (Exception e) {
      System.out.println(" send GET Request exception! " + e);
      e.printStackTrace();
    }
    //  use finally Block to close the input stream 
    finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    }
    return result;
  }

  /**
   *  To specify the  URL  send POST Method request 
   * 
   * @param url
   *       requesting  URL
   * @param param
   *       Request parameter, request parameter should be  name1=value1&name2=value2  In the form. 
   * @return  The response result of the remote resource represented 
   */
  public static String sendPost(String url, String param) {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
      URL realUrl = new URL(url);
      //  Open and URL Connection between 
      URLConnection conn = realUrl.openConnection();
      //  Sets the generic request properties 
      conn.setRequestProperty("accept", "*/*");
      conn.setRequestProperty("connection", "Keep-Alive");
      conn.setRequestProperty("user-agent",
          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
      //  send POST The request must be set to the following two lines 
      conn.setDoOutput(true);
      conn.setDoInput(true);
      //  To obtain URLConnection The output stream corresponding to the object 
      out = new PrintWriter(conn.getOutputStream());
      //  Send request parameter 
     
      out.print(param);
      // flush Buffering of the output stream 
      out.flush();
      //  define BufferedReader Input stream to read URL The response of the 
      in = new BufferedReader(
          new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line;
      }
    } catch (Exception e) {
      System.out.println(" send  POST  Request exception! "+e);
      e.printStackTrace();
    }
    // use finally Block to close the output stream, input stream 
    finally{
      try{
        if(out!=null){
          out.close();
        }
        if(in!=null){
          in.close();
        }
      }
      catch(IOException ex){
        ex.printStackTrace();
      }
    }
    
    try {
      result= new String(result.getBytes("ISO8859-1"),"UTF-8");
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return result;
  }  
}

2. Official DEMO httpClient request code


//import java.io.FileInputStream;
//import java.io.FileNotFoundException;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import org.dom4j.Document;  
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;  
import org.dom4j.Element;  

public class sendsms {
  
  private static String Url = "http://121.199.?.178/webservice/sms.php?method=Submit";
  
  
  
  public static void main(String [] args) {
    
    HttpClient client = new HttpClient(); 
    PostMethod method = new PostMethod(Url); 
      
    //client.getParams().setContentCharset("GBK");    
    client.getParams().setContentCharset("UTF-8");
    method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");

    String content = new String(" Your verification code is: 7528 . Please do not disclose the captcha to others. "); 
    
    NameValuePair[] data = {// Submit SMS 
        new NameValuePair("account", " The user name "), 
        new NameValuePair("password", " password "), // Passwords can be either plaintext or used 32 position MD5 encryption 
        //new NameValuePair("password", util.StringUtil.MD5Encode(" password ")),
        new NameValuePair("mobile", " Mobile phone number "), 
        new NameValuePair("content", content),
    };
    
    method.setRequestBody(data);    
    
    
    try {
      client.executeMethod(method);  
      
      String SubmitResult =method.getResponseBodyAsString();
          
      //System.out.println(SubmitResult);

      Document doc = DocumentHelper.parseText(SubmitResult); 
      Element root = doc.getRootElement();


      String code = root.elementText("code");  
      String msg = root.elementText("msg");  
      String smsid = root.elementText("smsid");  
      
      
      System.out.println(code);
      System.out.println(msg);
      System.out.println(smsid);
            
      if(code == "2"){
        System.out.println(" SMS submission successful ");
      }
      
    } catch (HttpException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (DocumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }  
    

  }
  0

3. Call the encapsulated httprequest code


String  phoneMessageParameter=new String("account=?&password=wxhdcs@456&content= Your check code is: [variable]. Please do not disclose the checksum to others. &mobile=?&stime=2012-08-01%208:20:23&sign=?&type=pt&extno=");
String returnResult=HttpRequest.sendPost("http://121.?.16.178/webservice/sms.php?method=Submit", phoneMessageParameter);
out.println("<script> alert("+returnResult+");</script>");

If you use this platform to pay attention to, is the parameter name is wrong in his official documents, send DEMO is right, and his interface was written in webserver, return not json or XML data, but rather a standard html page, and then the content of the need to write in html tag, if is test content message, this parameter must write their rules, otherwise an error, if formal purchase to set their own template content.


Related articles: