Java获取http和https协议返回的json数据
现在很多公司都是将数据返回一个json,而且很多第三方接口都是返回json数据,而且还需要使用到http协议,http协议是属于为加密的协议,而https协议需要SSL证书,https是将用户返回的信息加密处理,然而我们要获取这些数据,就需要引入SSL证书。现在我提供两个方法,帮助各位如何获取http和https返回的数据。
获取http协议的数据的方法,如下:
publicstaticJSONObjecthttpRequest(StringrequestUrl,StringrequestMethod){ JSONObjectjsonObject=null; StringBufferbuffer=newStringBuffer(); try{ URLurl=newURL(requestUrl); //http协议传输 HttpURLConnectionhttpUrlConn=(HttpURLConnection)url.openConnection(); httpUrlConn.setDoOutput(true); httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false); //设置请求方式(GET/POST) httpUrlConn.setRequestMethod(requestMethod); if("GET".equalsIgnoreCase(requestMethod)) httpUrlConn.connect(); //将返回的输入流转换成字符串 InputStreaminputStream=httpUrlConn.getInputStream(); InputStreamReaderinputStreamReader=newInputStreamReader(inputStream,"utf-8"); BufferedReaderbufferedReader=newBufferedReader(inputStreamReader); Stringstr=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(Exceptione){ e.printStackTrace(); } returnjsonObject; }
获取https协议的数据的方法,如下:
publicstaticJSONObjecthttpsRequest(StringrequestUrl,StringrequestMethod,StringoutputStr){ JSONObjectjsonObject=null; StringBufferbuffer=newStringBuffer(); try{ //创建SSLContext对象,并使用我们指定的信任管理器初始化 TrustManager[]tm={newMyX509TrustManager()}; SSLContextsslContext=SSLContext.getInstance("SSL","SunJSSE"); sslContext.init(null,tm,newjava.security.SecureRandom()); //从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactoryssf=sslContext.getSocketFactory(); URLurl=newURL(requestUrl); HttpsURLConnectionhttpUrlConn=(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){ OutputStreamoutputStream=httpUrlConn.getOutputStream(); //注意编码格式,防止中文乱码 outputStream.write(outputStr.getBytes("UTF-8")); outputStream.close(); } //将返回的输入流转换成字符串 InputStreaminputStream=httpUrlConn.getInputStream(); InputStreamReaderinputStreamReader=newInputStreamReader(inputStream,"utf-8"); BufferedReaderbufferedReader=newBufferedReader(inputStreamReader); Stringstr=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(ConnectExceptionce){ log.error("Weixinserverconnectiontimedout."); }catch(Exceptione){ log.error("httpsrequesterror:{}",e); } returnjsonObject; }
获取https协议的数据和获取http协议的区别在于
//创建SSLContext对象,并使用我们指定的信任管理器初始化 TrustManager[]tm={newMyX509TrustManager()}; SSLContextsslContext=SSLContext.getInstance("SSL","SunJSSE"); sslContext.init(null,tm,newjava.security.SecureRandom()); //从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactoryssf=sslContext.getSocketFactory(); URLurl=newURL(requestUrl); HttpsURLConnectionhttpUrlConn=(HttpsURLConnection)url.openConnection(); httpUrlConn.setSSLSocketFactory(ssf);
大家有更好的方法欢迎留言分享,以上就是本次共享的内容。还有,提示一下,如果复制中,缺失jar包,请自行下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。