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

Java获取http和https协议返回的json数据

程序员文章站 2024-03-08 09:36:30
现在很多公司都是将数据返回一个json,而且很多第三方接口都是返回json数据,而且还需要使用到http协议,http协议是属于为加密的协议,而https协议需要ssl证书...

现在很多公司都是将数据返回一个json,而且很多第三方接口都是返回json数据,而且还需要使用到http协议,http协议是属于为加密的协议,而https协议需要ssl证书,https是将用户返回的信息加密处理,然而我们要获取这些数据,就需要引入ssl证书。现在我提供两个方法,帮助各位如何获取http和https返回的数据。

获取http协议的数据的方法,如下:

public static jsonobject httprequest(string requesturl, string requestmethod) {
    jsonobject jsonobject = null;
    stringbuffer buffer = new stringbuffer();
    try {

      url url = new url(requesturl);
      // http协议传输
      httpurlconnection httpurlconn = (httpurlconnection) url.openconnection();

      httpurlconn.setdooutput(true);
      httpurlconn.setdoinput(true);
      httpurlconn.setusecaches(false);
      // 设置请求方式(get/post)
      httpurlconn.setrequestmethod(requestmethod);

      if ("get".equalsignorecase(requestmethod))
        httpurlconn.connect();
      // 将返回的输入流转换成字符串
      inputstream inputstream = httpurlconn.getinputstream();
      inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8");
      bufferedreader bufferedreader = new bufferedreader(inputstreamreader);

      string str = null;
      while ((str = bufferedreader.readline()) != null) {
        buffer.append(str);
      }
      bufferedreader.close();
      inputstreamreader.close();
      // 释放资源
      inputstream.close();
      inputstream = null;
      httpurlconn.disconnect();
      jsonobject = jsonobject.fromobject(buffer.tostring());
    } catch (exception e) {
      e.printstacktrace();
    }
    return jsonobject;
  }

获取https协议的数据的方法,如下:

public static jsonobject httpsrequest(string requesturl, string requestmethod, string outputstr) {
    jsonobject jsonobject = null;
    stringbuffer buffer = new stringbuffer();
    try {
      // 创建sslcontext对象,并使用我们指定的信任管理器初始化
      trustmanager[] tm = { new myx509trustmanager() };
      sslcontext sslcontext = sslcontext.getinstance("ssl", "sunjsse");
      sslcontext.init(null, tm, new java.security.securerandom());
      // 从上述sslcontext对象中得到sslsocketfactory对象
      sslsocketfactory ssf = sslcontext.getsocketfactory();

      url url = new url(requesturl);
      httpsurlconnection httpurlconn = (httpsurlconnection) url.openconnection();
      httpurlconn.setsslsocketfactory(ssf);

      httpurlconn.setdooutput(true);
      httpurlconn.setdoinput(true);
      httpurlconn.setusecaches(false);
      // 设置请求方式(get/post)
      httpurlconn.setrequestmethod(requestmethod);

      if ("get".equalsignorecase(requestmethod))
        httpurlconn.connect();

      // 当有数据需要提交时
      if (null != outputstr) {
        outputstream outputstream = httpurlconn.getoutputstream();
        // 注意编码格式,防止中文乱码
        outputstream.write(outputstr.getbytes("utf-8"));
        outputstream.close();
      }

      // 将返回的输入流转换成字符串
      inputstream inputstream = httpurlconn.getinputstream();
      inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8");
      bufferedreader bufferedreader = new bufferedreader(inputstreamreader);

      string str = null;
      while ((str = bufferedreader.readline()) != null) {
        buffer.append(str);
      }
      bufferedreader.close();
      inputstreamreader.close();
      // 释放资源
      inputstream.close();
      inputstream = null;
      httpurlconn.disconnect();
      jsonobject = jsonobject.fromobject(buffer.tostring());
    } catch (connectexception ce) {
      log.error("weixin server connection timed out.");
    } catch (exception e) {
      log.error("https request error:{}", e);
    }
    return jsonobject;

  }

获取https协议的数据和获取http协议的区别在于

  // 创建sslcontext对象,并使用我们指定的信任管理器初始化
      trustmanager[] tm = { new myx509trustmanager() };
      sslcontext sslcontext = sslcontext.getinstance("ssl", "sunjsse");
      sslcontext.init(null, tm, new java.security.securerandom());
      // 从上述sslcontext对象中得到sslsocketfactory对象
      sslsocketfactory ssf = sslcontext.getsocketfactory();

      url url = new url(requesturl);
      httpsurlconnection httpurlconn = (httpsurlconnection) url.openconnection();
      httpurlconn.setsslsocketfactory(ssf);

大家有更好的方法欢迎留言分享,以上就是本次共享的内容 。还有,提示一下,如果复制中,缺失jar包,请自行下载

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