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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
package de.bjusystems.vdrmanager.utils.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
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.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
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.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
/**
* Apache HttpClient helper class for performing HTTP requests.
*
* This class is intentionally *not* bound to any Android classes so that it is easier
* to develop and test. Use calls to this class inside Android AsyncTask implementations
* (or manual Thread-Handlers) to make HTTP requests asynchronous and not block the UI Thread.
*
* TODO cookies
* TODO multi-part binary data
* TODO follow 302s?
* TODO shutdown connection mgr? - client.getConnectionManager().shutdown();
*
* @author ccollins
*
*/
public class HttpHelper {
private static final String CONTENT_TYPE = "Content-Type";
private static final int POST_TYPE = 1;
private static final int GET_TYPE = 2;
private static final String GZIP = "gzip";
private static final String ACCEPT_ENCODING = "Accept-Encoding";
public static final String MIME_FORM_ENCODED = "application/x-www-form-urlencoded";
public static final String MIME_TEXT_PLAIN = "text/plain";
public static final String HTTP_RESPONSE = "HTTP_RESPONSE";
public static final String HTTP_RESPONSE_ERROR = "HTTP_RESPONSE_ERROR";
private final DefaultHttpClient client;
private final ResponseHandler<String> responseHandler;
/**
* Constructor.
*
*/
public HttpHelper() {
final HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.UTF_8);
params.setParameter(CoreProtocolPNames.USER_AGENT, "Apache-HttpClient/Android");
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
client = new DefaultHttpClient(cm, params);
// add gzip decompressor to handle gzipped content in responses
// (default we *do* always send accept encoding gzip header in request)
client.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
final HttpEntity entity = response.getEntity();
final Header contentEncodingHeader = entity.getContentEncoding();
if (contentEncodingHeader != null) {
final HeaderElement[] codecs = contentEncodingHeader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase(HttpHelper.GZIP)) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
});
responseHandler = new BasicResponseHandler();
}
/**
* Perform a simple HTTP GET operation.
*
*/
public String performGet(final String url) {
return performRequest(null, url, null, null, null, null, HttpHelper.GET_TYPE);
}
/**
* Perform an HTTP GET operation with user/pass and headers.
*
*/
public String performGet(final String url, final String user, final String pass,
final Map<String, String> additionalHeaders) {
return performRequest(null, url, user, pass, additionalHeaders, null, HttpHelper.GET_TYPE);
}
/**
* Perform a simplified HTTP POST operation.
*
*/
public String performPost(final String url, final Map<String, String> params) {
return performRequest(HttpHelper.MIME_FORM_ENCODED, url, null, null, null, params, HttpHelper.POST_TYPE);
}
/**
* Perform an HTTP POST operation with user/pass, headers, request
parameters,
* and a default content-type of "application/x-www-form-urlencoded."
*
*/
public String performPost(final String url, final String user, final String pass,
final Map<String, String> additionalHeaders, final Map<String, String> params) {
return performRequest(HttpHelper.MIME_FORM_ENCODED, url, user, pass, additionalHeaders, params,
HttpHelper.POST_TYPE);
}
/**
* Perform an HTTP POST operation with flexible parameters (the
complicated/flexible version of the method).
*
*/
public String performPost(final String contentType, final String url, final String user, final String pass,
final Map<String, String> additionalHeaders, final Map<String, String> params) {
return performRequest(contentType, url, user, pass, additionalHeaders, params, HttpHelper.POST_TYPE);
}
//
// private methods
//
private String performRequest(final String contentType, final String url, final String user, final String pass,
final Map<String, String> headers, final Map<String, String> params, final int requestType) {
// add user and pass to client credentials if present
if ((user != null) && (pass != null)) {
client.getCredentialsProvider().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(user, pass));
}
// process headers using request interceptor
final Map<String, String> sendHeaders = new HashMap<String, String>();
// add encoding header for gzip if not present
if (!sendHeaders.containsKey(HttpHelper.ACCEPT_ENCODING)) {
sendHeaders.put(HttpHelper.ACCEPT_ENCODING, HttpHelper.GZIP);
}
if ((headers != null) && (headers.size() > 0)) {
sendHeaders.putAll(headers);
}
if (requestType == HttpHelper.POST_TYPE) {
sendHeaders.put(HttpHelper.CONTENT_TYPE, contentType);
}
if (sendHeaders.size() > 0) {
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
for (final String key : sendHeaders.keySet()) {
if (!request.containsHeader(key)) {
request.addHeader(key, sendHeaders.get(key));
}
}
}
});
}
// handle POST or GET request respectively
HttpRequestBase method = null;
if (requestType == HttpHelper.POST_TYPE) {
method = new HttpPost(url);
// data - name/value params
List<NameValuePair> nvps = null;
if ((params != null) && (params.size() > 0)) {
nvps = new ArrayList<NameValuePair>();
for (final Map.Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
if (nvps != null) {
try {
final HttpPost methodPost = (HttpPost) method;
methodPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException("Error peforming HTTP request: " + e.getMessage(), e);
}
}
} else if (requestType == HttpHelper.GET_TYPE) {
method = new HttpGet(url);
}
// execute request
return execute(method);
}
private synchronized String execute(final HttpRequestBase method) {
String response = null;
// execute method returns?!? (rather than async) - do it here sync, and wrap async elsewhere
try {
response = client.execute(method, responseHandler);
} catch (final ClientProtocolException e) {
response = HttpHelper.HTTP_RESPONSE_ERROR + " - " + e.getClass().getSimpleName() + " " + e.getMessage();
//e.printStackTrace();
} catch (final IOException e) {
response = HttpHelper.HTTP_RESPONSE_ERROR + " - " + e.getClass().getSimpleName() + " " + e.getMessage();
//e.printStackTrace();
}
return response;
}
static class GzipDecompressingEntity extends HttpEntityWrapper {
public GzipDecompressingEntity(final HttpEntity entity) {
super(entity);
}
@Override
public InputStream getContent() throws IOException, IllegalStateException {
// the wrapped entity's getContent() decides about repeatability
final InputStream wrappedin = wrappedEntity.getContent();
return new GZIPInputStream(wrappedin);
}
@Override
public long getContentLength() {
// length of ungzipped content is not known
return -1;
}
}
}
|