Java-based SMS authentication code API calling code instance _java

Source: Internet
Author: User
Tags urlencode stringbuffer

This example of this article for you to share the Java SMS Authentication Code API code, for your reference, the specific content as follows

Import Java.io.BufferedReader;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.net.URLEncoder;
Import Java.util.HashMap;
 
Import Java.util.Map;
 
Import Net.sf.json.JSONObject; /** * SMS API Service Invoke sample code-aggregation data * Online interface Documentation: HTTP://WWW.JUHE.CN/DOCS/54 **/public class Juhedemo {public static final String DE
  F_chatset = "UTF-8";
  public static final int def_conn_timeout = 30000;
  public static final int def_read_timeout = 30000; public static String useragent = "mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/29.0.1547.66
 
  Safari/537.36 ";
 
  Configure the key public static final String Appkey = "*************************" that you requested;
    1. Screen word check to test public static void GetRequest1 () {String result =null; String url = "Http://v.juhe.cn/sms/black";//Request interface address MAP params = new HashMap ();//PleaseAsk the parameter params.put ("word", "");//need to detect SMS content, need to UTF8 UrlEncode params.put ("key", appkey);//apply Appkey (Apply Detail page query) try
      {result =net (URL, params, ' get ');
      Jsonobject object = Jsonobject.fromobject (result);
      if (Object.getint ("Error_code") ==0) {System.out.println (Object.get ("result"));
      }else{System.out.println (Object.get ("Error_code") + ":" +object.get ("Reason"));
    } catch (Exception e) {e.printstacktrace ();
    }//2. Send sms public static void GetRequest2 () {String result =null; String url = "Http://v.juhe.cn/sms/send";//Request interface address MAP params = new HashMap ()//Request parameter Params.put ("mobile", "");//Receive Short Letter of mobile phone number params.put ("tpl_id", "");//SMS template ID, please refer to the Personal Center SMS template Settings params.put ("Tpl_value", ""); variable name and variable value pairs. If you have a variable name or variable value with any of the special symbols in the #&=, please urlencode the code before passing, <a href= "HTTP://WWW.JUHE.CN/NEWS/INDEX/ID/50" _blank "> Detailed description ></a> params.put (" key ", Appkey)//Apply Appkey (Apply detailed page query) params.put (" Dtype ","//Returns the format of the data, XML or JSON, the default JSON try {result =net (URL, params, ' get ');
      Jsonobject object = Jsonobject.fromobject (result);
      if (Object.getint ("Error_code") ==0) {System.out.println (Object.get ("result"));
      }else{System.out.println (Object.get ("Error_code") + ":" +object.get ("Reason"));
    } catch (Exception e) {e.printstacktrace ();
   } public static void Main (string[] args) {}/** * * @param strurl Request address * @param params request parameters * @param method Request Methods * @return Network Request String * @throws Exception/public static string net (string strurl, Map pa
    Rams,string method) throws Exception {HttpURLConnection conn = null;
    BufferedReader reader = null;
    String rs = null;
      try {stringbuffer sb = new StringBuffer (); if (Method==null | | method.equals ("get")) {strURL = strurl+ "?"
      +urlencode (params);
      URL url = new URL (strurl); conn = (httpurlconnection) url.openconnectIon ();
      if (Method==null | | method.equals ("get")) {Conn.setrequestmethod (' get ');
        }else{Conn.setrequestmethod ("POST");
      Conn.setdooutput (TRUE);
      } conn.setrequestproperty ("User-agent", useragent);
      Conn.setusecaches (FALSE);
      Conn.setconnecttimeout (def_conn_timeout);
      Conn.setreadtimeout (def_read_timeout);
      Conn.setinstancefollowredirects (FALSE);
      Conn.connect (); if (params!= null && method.equals ("POST")) {try {dataoutputstream out = new DataOutputStream (
            Conn.getoutputstream ());
        Out.writebytes (UrlEncode (params)); catch (Exception e) {//Todo:handle Exception}} InputStream is = Conn.getinputstream ()
      ;
      reader = new BufferedReader (new InputStreamReader (IS, def_chatset));
      String strread = null;
      while ((Strread = Reader.readline ())!= null) {sb.append (strread);
    rs = sb.tostring (); catch (IOexception e) {e.printstacktrace ();
      finally {if (reader!= null) {reader.close ();
      } if (conn!= null) {conn.disconnect ();
  } return RS; ///Convert MAP type to request parameter public static String UrlEncode (map<string,object>data) {StringBuilder sb = new Stringbui
    Lder (); For (Map.Entryi:data.entrySet ()) {try {sb.append (I.getkey ()). Append ("="). Append (Urlencoder.encode
      Alue () + "", "UTF-8"). Append ("&");
      catch (Unsupportedencodingexception e) {e.printstacktrace ();
  } return sb.tostring (); }
}

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.