16
16
17
17
package org .kymjs .kjframe .http ;
18
18
19
+ import org .apache .http .Header ;
20
+ import org .apache .http .HttpEntity ;
21
+ import org .apache .http .HttpResponse ;
22
+ import org .apache .http .ProtocolVersion ;
23
+ import org .apache .http .StatusLine ;
24
+ import org .apache .http .entity .BasicHttpEntity ;
25
+ import org .apache .http .message .BasicHeader ;
26
+ import org .apache .http .message .BasicHttpResponse ;
27
+ import org .apache .http .message .BasicStatusLine ;
28
+ import org .kymjs .kjframe .http .Request .HttpMethod ;
29
+
19
30
import java .io .DataOutputStream ;
20
31
import java .io .IOException ;
21
32
import java .io .InputStream ;
29
40
import javax .net .ssl .HttpsURLConnection ;
30
41
import javax .net .ssl .SSLSocketFactory ;
31
42
32
- import org .apache .http .Header ;
33
- import org .apache .http .HttpEntity ;
34
- import org .apache .http .HttpResponse ;
35
- import org .apache .http .ProtocolVersion ;
36
- import org .apache .http .StatusLine ;
37
- import org .apache .http .entity .BasicHttpEntity ;
38
- import org .apache .http .message .BasicHeader ;
39
- import org .apache .http .message .BasicHttpResponse ;
40
- import org .apache .http .message .BasicStatusLine ;
41
- import org .kymjs .kjframe .http .Request .HttpMethod ;
42
-
43
43
/**
44
44
* HttpUrlConnection方式实现
45
45
*/
@@ -66,14 +66,14 @@ public HttpConnectStack(UrlRewriter urlRewriter) {
66
66
}
67
67
68
68
public HttpConnectStack (UrlRewriter urlRewriter ,
69
- SSLSocketFactory sslSocketFactory ) {
69
+ SSLSocketFactory sslSocketFactory ) {
70
70
mUrlRewriter = urlRewriter ;
71
71
mSslSocketFactory = sslSocketFactory ;
72
72
}
73
73
74
74
@ Override
75
75
public HttpResponse performRequest (Request <?> request ,
76
- Map <String , String > additionalHeaders ) throws IOException {
76
+ Map <String , String > additionalHeaders ) throws IOException {
77
77
String url = request .getUrl ();
78
78
HashMap <String , String > map = new HashMap <String , String >();
79
79
map .putAll (request .getHeaders ());
@@ -147,55 +147,61 @@ private HttpURLConnection openConnection(URL url, Request<?> request)
147
147
connection .setDoInput (true );
148
148
149
149
// use caller-provided custom SslSocketFactory, if any, for HTTPS
150
- if ("https" .equals (url .getProtocol ()) && mSslSocketFactory != null ) {
151
- ((HttpsURLConnection ) connection )
152
- .setSSLSocketFactory (mSslSocketFactory );
150
+ if ("https" .equals (url .getProtocol ())) {
151
+ if (mSslSocketFactory != null ) {
152
+ ((HttpsURLConnection ) connection )
153
+ .setSSLSocketFactory (mSslSocketFactory );
154
+ } else {
155
+ //信任所有证书
156
+ HTTPSTrustManager .allowAllSSL ();
157
+ }
153
158
}
154
159
155
160
return connection ;
156
161
}
157
162
158
- /* package */ static void setConnectionParametersForRequest (
163
+ /* package */
164
+ static void setConnectionParametersForRequest (
159
165
HttpURLConnection connection , Request <?> request )
160
166
throws IOException {
161
167
switch (request .getMethod ()) {
162
- case HttpMethod .GET :
163
- connection .setRequestMethod ("GET" );
164
- break ;
165
- case HttpMethod .DELETE :
166
- connection .setRequestMethod ("DELETE" );
167
- break ;
168
- case HttpMethod .POST :
169
- connection .setRequestMethod ("POST" );
170
- addBodyIfExists (connection , request );
171
- break ;
172
- case HttpMethod .PUT :
173
- connection .setRequestMethod ("PUT" );
174
- addBodyIfExists (connection , request );
175
- break ;
176
- case HttpMethod .HEAD :
177
- connection .setRequestMethod ("HEAD" );
178
- break ;
179
- case HttpMethod .OPTIONS :
180
- connection .setRequestMethod ("OPTIONS" );
181
- break ;
182
- case HttpMethod .TRACE :
183
- connection .setRequestMethod ("TRACE" );
184
- break ;
185
- case HttpMethod .PATCH :
186
- connection .setRequestMethod ("PATCH" );
187
- addBodyIfExists (connection , request );
188
- break ;
189
- default :
190
- throw new IllegalStateException ("Unknown method type." );
168
+ case HttpMethod .GET :
169
+ connection .setRequestMethod ("GET" );
170
+ break ;
171
+ case HttpMethod .DELETE :
172
+ connection .setRequestMethod ("DELETE" );
173
+ break ;
174
+ case HttpMethod .POST :
175
+ connection .setRequestMethod ("POST" );
176
+ addBodyIfExists (connection , request );
177
+ break ;
178
+ case HttpMethod .PUT :
179
+ connection .setRequestMethod ("PUT" );
180
+ addBodyIfExists (connection , request );
181
+ break ;
182
+ case HttpMethod .HEAD :
183
+ connection .setRequestMethod ("HEAD" );
184
+ break ;
185
+ case HttpMethod .OPTIONS :
186
+ connection .setRequestMethod ("OPTIONS" );
187
+ break ;
188
+ case HttpMethod .TRACE :
189
+ connection .setRequestMethod ("TRACE" );
190
+ break ;
191
+ case HttpMethod .PATCH :
192
+ connection .setRequestMethod ("PATCH" );
193
+ addBodyIfExists (connection , request );
194
+ break ;
195
+ default :
196
+ throw new IllegalStateException ("Unknown method type." );
191
197
}
192
198
}
193
199
194
200
/**
195
201
* 如果有body则添加
196
202
*/
197
203
private static void addBodyIfExists (HttpURLConnection connection ,
198
- Request <?> request ) throws IOException {
204
+ Request <?> request ) throws IOException {
199
205
byte [] body = request .getBody ();
200
206
if (body != null ) {
201
207
connection .setDoOutput (true );
0 commit comments