我是靠谱客的博主 刻苦流沙,这篇文章主要介绍有关https请求的工具类,现在分享给大家,希望可以做个参考。

有关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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/** * GET请求方式,无参数 * @param httpsUrl * @return */ public static String httpsGets(String httpsUrl){ BufferedReader input = null; StringBuilder sb = null; URL url = null; HttpURLConnection con = null; try { url = new URL(httpsUrl); try { // trust all hosts trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); if (url.getProtocol().toLowerCase().equals("https")) { https.setHostnameVerifier(DO_NOT_VERIFY); con = https; } else { con = (HttpURLConnection) url.openConnection(); } input = new BufferedReader(new InputStreamReader(con.getInputStream())); sb = new StringBuilder(); String s; while ((s = input.readLine()) != null) { sb.append(s).append("n"); } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e1) { e1.printStackTrace(); } finally { // close buffered if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } // disconnecting releases the resources held by a connection so they may be closed or reused if (con != null) { con.disconnect(); } } return sb == null ? null : sb.toString(); } /** * GET请求方式,有参数 * @param httpUrl * @param param * @return */ public static String httpsGet(String httpUrl, String param) { BufferedReader input = null; StringBuilder sb = null; URL url = null; HttpURLConnection con = null; try { String urlNameString = httpUrl + "?" + param; url = new URL(urlNameString); try { // trust all hosts trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); if (url.getProtocol().toLowerCase().equals("https")) { https.setHostnameVerifier(DO_NOT_VERIFY); con = https; } else { con = (HttpURLConnection) url.openConnection(); } input = new BufferedReader(new InputStreamReader(con.getInputStream())); sb = new StringBuilder(); String s; while ((s = input.readLine()) != null) { sb.append(s).append("n"); } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e1) { e1.printStackTrace(); } finally { // close buffered if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } // disconnecting releases the resources held by a connection so they may be closed or reused if (con != null) { con.disconnect(); } } return sb == null ? null : sb.toString(); } final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; private static void trustAllHosts() { final String TAG = "trustAllHosts"; // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[] {}; } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { log.info(TAG, "checkClientTrusted"); } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { log.info(TAG, "checkServerTrusted"); } } }; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } }

最后

以上就是刻苦流沙最近收集整理的关于有关https请求的工具类的全部内容,更多相关有关https请求内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(109)

评论列表共有 0 条评论

立即
投稿
返回
顶部