8
8
import java .util .Map ;
9
9
import java .util .stream .Stream ;
10
10
11
+ import javax .net .ssl .HttpsURLConnection ;
12
+ import javax .net .ssl .SSLContext ;
11
13
import javax .net .ssl .SSLSocketFactory ;
14
+ import javax .net .ssl .TrustManager ;
15
+ import javax .net .ssl .X509TrustManager ;
16
+
17
+ import java .security .cert .CertificateException ;
18
+ import java .security .cert .X509Certificate ;
19
+ import javax .net .ssl .*;
20
+
12
21
13
22
import org .apache .commons .io .IOUtils ;
14
23
@@ -51,6 +60,17 @@ public static void main(String[] args) {
51
60
"Connection: close\r \n " +
52
61
"Upgrade-Insecure-Requests: 1\r \n " +
53
62
"" ;
63
+ //String httpservice = "https://oms.meizu.com:8443";
64
+ String raws2 = "GET /cas/captcha.htm HTTP/1.1\r \n " +
65
+ "Host: oms.meizu.com:8443\r \n " +
66
+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0\r \n " +
67
+ "Accept: */*\r \n " +
68
+ "Accept-Language: en-US,en;q=0.5\r \n " +
69
+ "Referer: https://oms.meizu.com:8443/cas/login?service=http%3A%2F%2Foms.meizu.com%2Flogin.action\r \n " +
70
+ "Cookie: JSESSIONID=9CA93BDD402AD7AA41962C577874B105; MZ_STORE_UUID=7508ed10-fa01-473f-9c1e-20fb05abe416; tj_coid=6391ea46eb6c0d45226b205940b0f353; CSRF_ID=3f7c2d42-8fe1-47e8-95b5-d2a129d9727d; MEIZUSTORECARTCOUNT=%7B%22c%22%3A0%2C%22t%22%3A1509602002356%2C%22s%22%3Afalse%7D\r \n " +
71
+ "Connection: close\r \n " +
72
+ "Cache-Control: max-age=0\r \n " +
73
+ "" ;
54
74
RequestHelper x = new RequestHelper ();
55
75
x .httpservice = httpservice ;
56
76
x .raws =raws ;
@@ -63,7 +83,7 @@ public static void main(String[] args) {
63
83
}
64
84
}
65
85
66
- public byte [] readStream (InputStream inStream ) throws Exception { //这个方法有点问题,图片只有一半
86
+ public byte [] readStream (InputStream inStream ) throws Exception { //��������е����⣬ͼƬֻ��һ��
67
87
int count = 0 ;
68
88
while (count == 0 ) {
69
89
count = inStream .available ();
@@ -134,25 +154,68 @@ public void parser() {
134
154
}
135
155
}
136
156
157
+ private static TrustManager myX509TrustManager = new X509TrustManager () {
158
+
159
+ @ Override
160
+ public X509Certificate [] getAcceptedIssuers () {
161
+ return null ;
162
+ }
163
+
164
+ @ Override
165
+ public void checkServerTrusted (X509Certificate [] chain , String authType )
166
+ throws CertificateException {
167
+ }
168
+
169
+ @ Override
170
+ public void checkClientTrusted (X509Certificate [] chain , String authType )
171
+ throws CertificateException {
172
+ }
173
+ };
174
+
137
175
public byte [] dorequest () throws Exception {
138
176
139
177
140
178
try {
141
- URL url = new URL (this .strurl );
142
- HttpURLConnection conn = (HttpURLConnection )url .openConnection ();
143
- for (Map .Entry <String , String > entry : this .headers .entrySet ()) {
144
- conn .addRequestProperty (entry .getKey (),entry .getValue ());
145
- //conn.addRequestProperty("User-Agent","Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0");
179
+ if (this .strurl .startsWith ("https:" )) {
180
+ URL url = new URL (this .strurl );
181
+ // 创建SSLContext对象,并使用我们指定的信任管理器初始化
182
+ TrustManager [] tm = new TrustManager []{myX509TrustManager };
183
+ SSLContext sslContext = SSLContext .getInstance ("SSL" , "SunJSSE" );
184
+ sslContext .init (null , tm , new java .security .SecureRandom ());
185
+ // 从上述SSLContext对象中得到SSLSocketFactory对象
186
+ SSLSocketFactory ssf = sslContext .getSocketFactory ();
187
+ HttpsURLConnection conn = (HttpsURLConnection ) url .openConnection ();
188
+ conn .setSSLSocketFactory (ssf );
189
+ for (Map .Entry <String , String > entry : this .headers .entrySet ()) {
190
+ conn .addRequestProperty (entry .getKey (),entry .getValue ());
191
+ //conn.addRequestProperty("User-Agent","Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0");
192
+ }
193
+ conn .setRequestMethod (this .method );
194
+ conn .setConnectTimeout (5 * 1000 );
195
+ conn .setReadTimeout (8 *1000 );
196
+ InputStream inStream = conn .getInputStream ();
197
+ byte [] btImg = readInputStream (inStream );
198
+ return btImg ;
199
+ }
200
+ else {
201
+ URL url = new URL (this .strurl );
202
+ HttpURLConnection conn = (HttpURLConnection )url .openConnection ();
203
+ for (Map .Entry <String , String > entry : this .headers .entrySet ()) {
204
+ conn .addRequestProperty (entry .getKey (),entry .getValue ());
205
+ //conn.addRequestProperty("User-Agent","Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0");
206
+ }
207
+ conn .setRequestMethod (this .method );
208
+ conn .setConnectTimeout (5 * 1000 );
209
+ conn .setReadTimeout (8 *1000 );
210
+ InputStream inStream = conn .getInputStream ();
211
+ byte [] btImg = readInputStream (inStream );
212
+ return btImg ;
146
213
}
147
- conn .setRequestMethod (this .method );
148
- conn .setConnectTimeout (5 * 1000 );
149
- conn .setReadTimeout (8 *1000 );
150
- InputStream inStream = conn .getInputStream ();//通过输入流获取图片数据
151
- byte [] btImg = readInputStream (inStream );//得到图片的二进制数据
152
- return btImg ;
214
+
153
215
} catch (Exception e ) {
154
216
System .out .println (e .getLocalizedMessage ());
217
+ return (e .toString ()).getBytes ();
155
218
}
156
- return null ;
219
+
157
220
}
158
221
}
0 commit comments