-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfHttpClient
More file actions
86 lines (76 loc) · 3.27 KB
/
SelfHttpClient
File metadata and controls
86 lines (76 loc) · 3.27 KB
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
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HttpContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
/**
* Created by wuchunhua on 2015/12/10.
*/
public class SelfHttpClient extends DefaultHttpClient {
private static ClientConnectionManager TT;
public SelfHttpClient() {
super(iA(), new BasicHttpParams());
addRequestInterceptor(new HttpRequestInterceptor() {
public void process(HttpRequest paramHttpRequest, HttpContext paramHttpContext)
throws HttpException, IOException {
if (!paramHttpRequest.containsHeader("Accept-Encoding"))
paramHttpRequest.addHeader("Accept-Encoding", "gzip");
}
});
addResponseInterceptor(new HttpResponseInterceptor() {
public void process(HttpResponse httpResponse, HttpContext paramHttpContext)
throws HttpException, IOException {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity == null) return;
Header header = httpEntity.getContentEncoding();
if (header == null) return;
HeaderElement[] arrheaderElement = header.getElements();
if (arrheaderElement == null) return;
int n = 0;
while (n < arrheaderElement.length) {
if (arrheaderElement[n].getName().equalsIgnoreCase("gzip")) {
httpResponse.setEntity((HttpEntity) new b(httpResponse.getEntity()));
return;
}
++n;
}
return;
}
});
}
static final ClientConnectionManager iA() {
SchemeRegistry localSchemeRegistry = new SchemeRegistry();
localSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
localSchemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
TT = new ThreadSafeClientConnManager(new BasicHttpParams(), localSchemeRegistry);
return TT;
}
private static class b extends HttpEntityWrapper {
public b(HttpEntity paramHttpEntity) {
super(paramHttpEntity);
}
public InputStream getContent()
throws IOException, IllegalStateException {
return new GZIPInputStream(this.wrappedEntity.getContent());
}
public long getContentLength() {
return -1L;
}
}
}