package com.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
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.HttpHost;
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.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnRoutePNames;
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.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.SyncBasicHttpContext;
import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.PreemtiveAuthorizationHttpRequestInterceptor;
/**
* code from AsyncHttpClient(https://github.com/loopj/android-async-http)
*
* @author ligl01
*
*/
public class LHttpClient {
public static final String LOG_TAG = "LHttpClient";
public static final String HEADER_CONTENT_TYPE = "Content-Type";
public static final String HEADER_CONTENT_RANGE = "Content-Range";
public static final String HEADER_CONTENT_ENCODING = "Content-Encoding";
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
public static final String ENCODING_GZIP = "gzip";
public static final int DEFAULT_MAX_CONNECTIONS = 10;
public static final int DEFAULT_TIMEOUT = 10 * 1000;
public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
public static final int DEFAULT_MAX_RETRIES = 5;
public static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500;
public static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
private int maxConnections = DEFAULT_MAX_CONNECTIONS;
private int connectTimeout = DEFAULT_TIMEOUT;
private int responseTimeout = DEFAULT_SOCKET_TIMEOUT;
private final DefaultHttpClient httpClient;
private final HttpContext httpContext;
private final Map<String, String> clientHeaderMap;
public LHttpClient() {
this(80, 443);
}
public LHttpClient(int httpPort, int httpsPort) {
BasicHttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams, connectTimeout);
ConnManagerParams.setMaxConnectionsPerRoute(httpParams,
new ConnPerRouteBean(maxConnections));
ConnManagerParams.setMaxTotalConnections(httpParams,
DEFAULT_MAX_CONNECTIONS);
HttpConnectionParams.setSoTimeout(httpParams, responseTimeout);
HttpConnectionParams.setConnectionTimeout(httpParams, connectTimeout);
HttpConnectionParams.setTcpNoDelay(httpParams, true);
HttpConnectionParams.setSocketBufferSize(httpParams,
DEFAULT_SOCKET_BUFFER_SIZE);
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), httpPort));
schemeRegistry.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), httpsPort));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
httpParams, schemeRegistry);
httpContext = new SyncBasicHttpContext(new BasicHttpContext());
httpClient = new DefaultHttpClient(cm, httpParams);
httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) {
if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
for (String header : clientHeaderMap.keySet()) {
request.addHeader(header, clientHeaderMap.get(header));
}
}
});
httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(HttpResponse response, HttpContext context) {
final HttpEntity entity = response.getEntity();
final Header encoding = entity.getContentEncoding();
if (encoding != null) {
for (HeaderElement element : encoding.getElements()) {
if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
response.setEntity(new InflatingEntity(response
.getEntity()));
break;
}
}
}
}
});
httpClient.setHttpRequestRetryHandler(new RetryHandler(
DEFAULT_MAX_RETRIES, DEFAULT_RETRY_SLEEP_TIME_MILLIS));
clientHeaderMap = new HashMap<String, String>();
}
/**
* Get the underlying HttpClient instance. This is useful for setting
* additional fine-grained settings for requests by accessing the client's
* ConnectionManager, HttpParams and SchemeRegistry.
*/
public HttpClient getHttpClient() {
return this.httpClient;
}
/**
* Get the underlying HttpContext instance. This is useful for getting and
* setting fine-grained settings for requests by accessing the context's
* attributes such as the CookieStore.
*
* @return underlying HttpContext instance
*/
public HttpContext getHttpContext() {
return this.httpContext;
}
/**
* Sets an optional CookieStore to use when making requests
*
* @param cookieStore
* The CookieStore implementation to use, usually an instance of
* {@link PersistentCookieStore}
*/
public void setCookieStore(CookieStore cookieStore) {
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}
/**
* Returns current connection timeout limit (milliseconds). By default, this
* is set to 10 seconds.
*
* @return Connection timeout limit in milliseconds
*/
public int getConnectTimeout() {
return connectTimeout;
}
/**
* Set connection timeout limit (milliseconds). By default, this is set to
* 10 seconds.
*
* @param value
* Connection timeout in milliseconds, minimal value is 1000 (1
* second).
*/
public void setConnectTimeout(int value) {
connectTimeout = value < 1000 ? DEFAULT_SOCKET_TIMEOUT : value;
final HttpParams httpParams = httpClient.getParams();
ConnManagerParams.setTimeout(httpParams, connectTimeout);
HttpConnectionParams.setConnectionTimeout(httpParams, connectTimeout);
}
/**
* Returns current response timeout limit (milliseconds). By default, this
* is set to 10 seconds.
*
* @return Response timeout limit in milliseconds
*/
public int getResponseTimeout() {
return responseTimeout;
}
/**
* Set response timeout limit (milliseconds). By default, this is set to 10
* seconds.
*
* @param value
* Resp