欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于JAVA的短信验证码api调用代码实例

程序员文章站 2024-03-11 19:47:55
本文实例为大家分享了java的短信验证码api调用代码,供大家参考,具体内容如下 import java.io.bufferedreader; import j...

本文实例为大家分享了java的短信验证码api调用代码,供大家参考,具体内容如下

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;
 
/**
*短信api服务调用示例代码 - 聚合数据
*在线接口文档:http://www.juhe.cn/docs/54
**/
 
public class juhedemo {
  public static final string def_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";
 
  //配置您申请的key
  public static final string appkey ="*************************";
 
  //1.屏蔽词检查测
  public static void getrequest1(){
    string result =null;
    string url ="http://v.juhe.cn/sms/black";//请求接口地址
    map params = new hashmap();//请求参数
      params.put("word","");//需要检测的短信内容,需要utf8 urlencode
      params.put("key",appkey);//应用appkey(应用详细页查询)
 
    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.发送短信
  public static void getrequest2(){
    string result =null;
    string url ="http://v.juhe.cn/sms/send";//请求接口地址
    map params = new hashmap();//请求参数
      params.put("mobile","");//接收短信的手机号码
      params.put("tpl_id","");//短信模板id,请参考个人中心短信模板设置
      params.put("tpl_value","");//变量名和变量值对。如果你的变量名或者变量值中带有#&=中的任意一个特殊符号,请先分别进行urlencode编码后再传递,<a href="http://www.juhe.cn/news/index/id/50" target="_blank">详细说明></a>
      params.put("key",appkey);//应用appkey(应用详细页查询)
      params.put("dtype","");//返回数据的格式,xml或json,默认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 请求地址
   * @param params 请求参数
   * @param method 请求方法
   * @return 网络请求字符串
   * @throws exception
   */
  public static string net(string strurl, map params,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;
  }
 
  //将map型转为请求参数型
  public static string urlencode(map<string,object>data) {
    stringbuilder sb = new stringbuilder();
    for (map.entryi : data.entryset()) {
      try {
        sb.append(i.getkey()).append("=").append(urlencoder.encode(i.getvalue()+"","utf-8")).append("&");
      } catch (unsupportedencodingexception e) {
        e.printstacktrace();
      }
    }
    return sb.tostring();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。