3939import org .apache .http .HttpResponseInterceptor ;
4040import org .apache .http .HttpVersion ;
4141import org .apache .http .client .CookieStore ;
42+ import org .apache .http .client .HttpClient ;
4243import org .apache .http .client .methods .HttpGet ;
4344import org .apache .http .client .methods .HttpPost ;
4445import org .apache .http .client .methods .HttpPut ;
7677 * For example:
7778 * <p>
7879 * <pre>
79- * AsyncHttpClient client = new AsyncHttpClient("My User Agent" );
80+ * AsyncHttpClient client = new AsyncHttpClient();
8081 * client.get("http://www.google.com", new AsyncHttpResponseHandler() {
8182 * @Override
8283 * public void onSuccess(String response) {
8687 * </pre>
8788 */
8889public class AsyncHttpClient {
90+ private static final String VERSION = "1.3.1" ;
91+
8992 private static final int DEFAULT_MAX_CONNECTIONS = 10 ;
9093 private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
9194 private static final int DEFAULT_MAX_RETRIES = 5 ;
92- private static final String ENCODING = "UTF-8" ;
95+ private static final String ENCODING = "UTF-8" ;
9396 private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
9497 private static final String ENCODING_GZIP = "gzip" ;
9598
@@ -101,11 +104,11 @@ public class AsyncHttpClient {
101104 private ThreadPoolExecutor threadPool ;
102105 private Map <Context , List <WeakReference <Future >>> requestMap ;
103106
107+
104108 /**
105- * Creates a new AsyncHttpClient which will identify itself with the user agent userAgent
106- * @param userAgent the identifier to use in the User-Agent header in requests
109+ * Creates a new AsyncHttpClient.
107110 */
108- public AsyncHttpClient (String userAgent ) {
111+ public AsyncHttpClient () {
109112 BasicHttpParams httpParams = new BasicHttpParams ();
110113
111114 ConnManagerParams .setTimeout (httpParams , socketTimeout );
@@ -116,7 +119,7 @@ public AsyncHttpClient(String userAgent) {
116119 HttpConnectionParams .setTcpNoDelay (httpParams , true );
117120
118121 HttpProtocolParams .setVersion (httpParams , HttpVersion .HTTP_1_1 );
119- HttpProtocolParams .setUserAgent (httpParams , userAgent );
122+ HttpProtocolParams .setUserAgent (httpParams , String . format ( "android-async-http/%s (http://loopj.com/android-async-http)" , VERSION ) );
120123
121124 SchemeRegistry schemeRegistry = new SchemeRegistry ();
122125 schemeRegistry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
@@ -155,6 +158,15 @@ public void process(HttpResponse response, HttpContext context) {
155158 requestMap = new WeakHashMap <Context , List <WeakReference <Future >>>();
156159 }
157160
161+ /**
162+ * Get the underlying HttpClient instance. This is useful for setting
163+ * additional fine-grained settings for requests by accessing the
164+ * client's ConnectionManager, HttpParams and SchemeRegistry.
165+ */
166+ public HttpClient getHttpClient () {
167+ return this .httpClient ;
168+ }
169+
158170 /**
159171 * Sets an optional CookieStore to use when making requests
160172 * @param cookieStore The CookieStore implementation to use, usually an instance of {@link PersistentCookieStore}
@@ -172,6 +184,24 @@ public void setThreadPool(ThreadPoolExecutor threadPool) {
172184 this .threadPool = threadPool ;
173185 }
174186
187+ /**
188+ * Sets the User-Agent header to be sent with each request. By default,
189+ * "Android Asynchronous Http Client/VERSION (http://loopj.com/android-async-http/)" is used.
190+ * @param userAgent the string to use in the User-Agent header.
191+ */
192+ public void setUserAgent (String userAgent ) {
193+ HttpProtocolParams .setUserAgent (this .httpClient .getParams (), userAgent );
194+ }
195+
196+ /**
197+ * Sets the SSLSocketFactory to user when making requests. By default,
198+ * a new, default SSLSocketFactory is used.
199+ * @param sslSocketFactory the socket factory to use for https requests.
200+ */
201+ public void setSSLSocketFactory (SSLSocketFactory sslSocketFactory ) {
202+ this .httpClient .getConnectionManager ().getSchemeRegistry ().register (new Scheme ("https" , sslSocketFactory , 443 ));
203+ }
204+
175205 /**
176206 * Cancels any pending (or potentially active) requests associated with the
177207 * passed Context.
0 commit comments