现在很多公司都是将数据返回一个json,而且很多第三方接口都是返回json数据,而且还需要使用到http协议,http协议是属于为加密的协议,而https协议需要SSL证书,https是将用户返回的信息加密处理,然而我们要获取这些数据,就需要引入SSL证书。现在我提供两个方法,帮助各位如何获取http和https返回的数据。
获取http协议的数据的方法,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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协议的数据的方法,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
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协议的区别在于
1
2
3
4
5
6
7
8
9
10
|
// 创建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包,请自行下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/wangtj-19/p/5889056.html