当前位置: 移动技术网 > IT编程>开发语言>Java > java servlet手机app访问接口(二)短信验证

java servlet手机app访问接口(二)短信验证

2019年07月22日  | 移动技术网IT编程  | 我要评论

今天找了几个短信平台,其实最想使用的一个是sharesdk,使用它上面http api短信功能,不仅价格低,而且最少可以充值100rmb,但是审核过于严格,对应app还必须集成他们的短信功能,而且要上传审核也得20多天,我也只是想找个短信平台测试下而已,所以它就算了。然后就在百度随便在好了一个短信平台www.wasun.cn,暂时感觉它还不错,至少它给的测试帐号接受短信的速度没超过5秒,我看了下一般是3秒甚至更快。 下面我就说说调用短信接口的方法,以及使用中途遇到的问题。

一、httprequest方式请求方法

他给的domo其实已经封装好方法的,是使用httpclient请求的,以前在.net中使用过这个类, 而且.net中还直接有httpwebrequest这个类,我看了下代码在java中它的功能应该是被封装到了urlconnection这个类里面,由于时间,封装的方法我也是从网上找的没深入研究,不过应该和.net中的httpwebrequest是一个含义。下面贴代码,包括demo代的httpclient这个类的代码也一同贴上。

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 {
  /**
   * 向指定url发送get方法的请求
   * 
   * @param url
   *      发送请求的url
   * @param param
   *      请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
   * @return url 所代表远程资源的响应结果
   */
  public static string sendget(string url, string param) {
    string result = "";
    bufferedreader in = null;
    
    try {
      string urlnamestring = url + "?" + param;
      url realurl = new url(urlnamestring);
      // 打开和url之间的连接
      urlconnection connection = realurl.openconnection();
      // 设置通用的请求属性
      connection.setrequestproperty("accept", "*/*");
      connection.setrequestproperty("connection", "keep-alive");
      connection.setrequestproperty("user-agent",
          "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
      // 建立实际的连接
      connection.connect();
      // 获取所有响应头字段
      map<string, list<string>> map = connection.getheaderfields();
      // 遍历所有的响应头字段
      for (string key : map.keyset()) {
        system.out.println(key + "--->" + map.get(key));
      }
      // 定义 bufferedreader输入流来读取url的响应
      in = new bufferedreader(new inputstreamreader(
          connection.getinputstream()));
      string line;
      while ((line = in.readline()) != null) {
        result += line;
      }
    } catch (exception e) {
      system.out.println("发送get请求出现异常!" + e);
      e.printstacktrace();
    }
    // 使用finally块来关闭输入流
    finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (exception e2) {
        e2.printstacktrace();
      }
    }
    return result;
  }

  /**
   * 向指定 url 发送post方法的请求
   * 
   * @param url
   *      发送请求的 url
   * @param param
   *      请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
   * @return 所代表远程资源的响应结果
   */
  public static string sendpost(string url, string param) {
    printwriter out = null;
    bufferedreader in = null;
    string result = "";
    try {
      url realurl = new url(url);
      // 打开和url之间的连接
      urlconnection conn = realurl.openconnection();
      // 设置通用的请求属性
      conn.setrequestproperty("accept", "*/*");
      conn.setrequestproperty("connection", "keep-alive");
      conn.setrequestproperty("user-agent",
          "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)");
      // 发送post请求必须设置如下两行
      conn.setdooutput(true);
      conn.setdoinput(true);
      // 获取urlconnection对象对应的输出流
      out = new printwriter(conn.getoutputstream());
      // 发送请求参数
     
      out.print(param);
      // flush输出流的缓冲
      out.flush();
      // 定义bufferedreader输入流来读取url的响应
      in = new bufferedreader(
          new inputstreamreader(conn.getinputstream()));
      string line;
      while ((line = in.readline()) != null) {
        result += line;
      }
    } catch (exception e) {
      system.out.println("发送 post 请求出现异常!"+e);
      e.printstacktrace();
    }
    //使用finally块来关闭输出流、输入流
    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;
  }  
}

二、官方demo httpclient方式请求代码

//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("您的验证码是:7528。请不要把验证码泄露给其他人。"); 
    
    namevaluepair[] data = {//提交短信
        new namevaluepair("account", "用户名"), 
        new namevaluepair("password", "密码"), //密码可以使用明文密码或使用32位md5加密
        //new namevaluepair("password", util.stringutil.md5encode("密码")),
        new namevaluepair("mobile", "手机号码"), 
        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("短信提交成功");
      }
      
    } 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

三、调用封装的httprequest代码 

string  phonemessageparameter=new string("account=?&password=wxhdcs@456&content=您的校验码是:【变量】。请不要把校验码泄露给其他人。&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>");

如果使用这个平台要注意下,就是他官方的文档中的参数名是错的,发的demo中才是正确的,还有他的接口是用webserver写的,返回的不是json或则xml数据,而是一个标准的html页面,然后需要的内容都写在html中的标签中, 如果是测试content短信内容这个参数必须写他们规定的,否则报错,如果正式购买可以自己定模版内容。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网