Home | History | Annotate | Download | only in client
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java $
      3  * $Revision: 677250 $
      4  * $Date: 2008-07-16 04:45:47 -0700 (Wed, 16 Jul 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl.client;
     33 
     34 import org.apache.http.ConnectionReuseStrategy;
     35 import org.apache.http.HttpVersion;
     36 import org.apache.http.auth.AuthSchemeRegistry;
     37 import org.apache.http.client.AuthenticationHandler;
     38 import org.apache.http.client.CookieStore;
     39 import org.apache.http.client.CredentialsProvider;
     40 import org.apache.http.client.HttpRequestRetryHandler;
     41 import org.apache.http.client.RedirectHandler;
     42 import org.apache.http.client.UserTokenHandler;
     43 import org.apache.http.client.params.AuthPolicy;
     44 import org.apache.http.client.params.ClientPNames;
     45 import org.apache.http.client.params.CookiePolicy;
     46 import org.apache.http.client.protocol.ClientContext;
     47 import org.apache.http.client.protocol.RequestAddCookies;
     48 import org.apache.http.client.protocol.RequestDefaultHeaders;
     49 import org.apache.http.client.protocol.RequestProxyAuthentication;
     50 import org.apache.http.client.protocol.RequestTargetAuthentication;
     51 import org.apache.http.client.protocol.ResponseProcessCookies;
     52 import org.apache.http.conn.ClientConnectionManager;
     53 import org.apache.http.conn.ClientConnectionManagerFactory;
     54 import org.apache.http.conn.ConnectionKeepAliveStrategy;
     55 import org.apache.http.conn.routing.HttpRoutePlanner;
     56 import org.apache.http.conn.scheme.PlainSocketFactory;
     57 import org.apache.http.conn.scheme.Scheme;
     58 import org.apache.http.conn.scheme.SchemeRegistry;
     59 import org.apache.http.conn.ssl.SSLSocketFactory;
     60 import org.apache.http.cookie.CookieSpecRegistry;
     61 import org.apache.http.impl.DefaultConnectionReuseStrategy;
     62 import org.apache.http.impl.auth.BasicSchemeFactory;
     63 import org.apache.http.impl.auth.DigestSchemeFactory;
     64 import org.apache.http.impl.conn.ProxySelectorRoutePlanner;
     65 import org.apache.http.impl.conn.SingleClientConnManager;
     66 import org.apache.http.impl.cookie.BestMatchSpecFactory;
     67 import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
     68 import org.apache.http.impl.cookie.NetscapeDraftSpecFactory;
     69 import org.apache.http.impl.cookie.RFC2109SpecFactory;
     70 import org.apache.http.impl.cookie.RFC2965SpecFactory;
     71 import org.apache.http.params.BasicHttpParams;
     72 import org.apache.http.params.HttpParams;
     73 import org.apache.http.params.HttpProtocolParams;
     74 import org.apache.http.protocol.BasicHttpContext;
     75 import org.apache.http.protocol.BasicHttpProcessor;
     76 import org.apache.http.protocol.HTTP;
     77 import org.apache.http.protocol.HttpContext;
     78 import org.apache.http.protocol.HttpRequestExecutor;
     79 import org.apache.http.protocol.RequestConnControl;
     80 import org.apache.http.protocol.RequestContent;
     81 import org.apache.http.protocol.RequestExpectContinue;
     82 import org.apache.http.protocol.RequestTargetHost;
     83 import org.apache.http.protocol.RequestUserAgent;
     84 import org.apache.http.util.VersionInfo;
     85 
     86 
     87 
     88 /**
     89  * Default implementation of an HTTP client.
     90  *
     91  * <h3>Prefer HttpURLConnection for new code</h3>
     92  * Android includes two HTTP clients: {@code HttpURLConnection} and Apache HTTP
     93  * Client. Both support HTTPS, streaming uploads and downloads, configurable
     94  * timeouts, IPv6 and connection pooling. Apache HTTP client has fewer bugs in
     95  * Android 2.2 (Froyo) and earlier releases. For Android 2.3 (Gingerbread) and
     96  * later, {@link java.net.HttpURLConnection HttpURLConnection} is the best
     97  * choice. Its simple API and small size makes it great fit for Android.
     98  * Transparent compression and response caching reduce network use, improve
     99  * speed and save battery. See the <a
    100  * href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">Android
    101  * Developers Blog</a> for a comparison of the two HTTP clients.
    102  *
    103  * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
    104  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
    105  *
    106  * <!-- empty lines to avoid svn diff problems -->
    107  * @version   $Revision: 677250 $
    108  *
    109  * @since 4.0
    110  *
    111  * @deprecated Please use {@link java.net.URL#openConnection} instead.
    112  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
    113  *     for further details.
    114  */
    115 @Deprecated
    116 public class DefaultHttpClient extends AbstractHttpClient {
    117 
    118 
    119     /**
    120      * Creates a new HTTP client from parameters and a connection manager.
    121      *
    122      * @param params    the parameters
    123      * @param conman    the connection manager
    124      */
    125     public DefaultHttpClient(
    126             final ClientConnectionManager conman,
    127             final HttpParams params) {
    128         super(conman, params);
    129     }
    130 
    131 
    132     public DefaultHttpClient(final HttpParams params) {
    133         super(null, params);
    134     }
    135 
    136 
    137     public DefaultHttpClient() {
    138         super(null, null);
    139     }
    140 
    141 
    142     @Override
    143     protected HttpParams createHttpParams() {
    144         HttpParams params = new BasicHttpParams();
    145         HttpProtocolParams.setVersion(params,
    146                 HttpVersion.HTTP_1_1);
    147         HttpProtocolParams.setContentCharset(params,
    148                 HTTP.DEFAULT_CONTENT_CHARSET);
    149 
    150         /*
    151          * Android note: Send each request body without first asking the server
    152          * whether it will be accepted. Asking first slows down the common case
    153          * and results in "417 expectation failed" errors when a HTTP/1.0 server
    154          * is behind a proxy. http://b/2471595
    155          */
    156         HttpProtocolParams.setUseExpectContinue(params,
    157                 false); // android-changed
    158 
    159         // determine the release version from packaged version info
    160         final VersionInfo vi = VersionInfo.loadVersionInfo
    161             ("org.apache.http.client", getClass().getClassLoader());
    162         final String release = (vi != null) ?
    163             vi.getRelease() : VersionInfo.UNAVAILABLE;
    164         HttpProtocolParams.setUserAgent(params,
    165                 "Apache-HttpClient/" + release + " (java 1.4)");
    166 
    167         return params;
    168     }
    169 
    170 
    171     @Override
    172     protected HttpRequestExecutor createRequestExecutor() {
    173         return new HttpRequestExecutor();
    174     }
    175 
    176 
    177     @Override
    178     protected ClientConnectionManager createClientConnectionManager() {
    179         SchemeRegistry registry = new SchemeRegistry();
    180         registry.register(
    181                 new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    182         registry.register(
    183                 new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    184 
    185         ClientConnectionManager connManager = null;
    186         HttpParams params = getParams();
    187 
    188         ClientConnectionManagerFactory factory = null;
    189 
    190         // Try first getting the factory directly as an object.
    191         factory = (ClientConnectionManagerFactory) params
    192                 .getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY);
    193         if (factory == null) { // then try getting its class name.
    194             String className = (String) params.getParameter(
    195                     ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
    196             if (className != null) {
    197                 try {
    198                     Class<?> clazz = Class.forName(className);
    199                     factory = (ClientConnectionManagerFactory) clazz.newInstance();
    200                 } catch (ClassNotFoundException ex) {
    201                     throw new IllegalStateException("Invalid class name: " + className);
    202                 } catch (IllegalAccessException ex) {
    203                     throw new IllegalAccessError(ex.getMessage());
    204                 } catch (InstantiationException ex) {
    205                     throw new InstantiationError(ex.getMessage());
    206                 }
    207             }
    208         }
    209 
    210         if(factory != null) {
    211             connManager = factory.newInstance(params, registry);
    212         } else {
    213             connManager = new SingleClientConnManager(getParams(), registry);
    214         }
    215 
    216         return connManager;
    217     }
    218 
    219 
    220     @Override
    221     protected HttpContext createHttpContext() {
    222         HttpContext context = new BasicHttpContext();
    223         context.setAttribute(
    224                 ClientContext.AUTHSCHEME_REGISTRY,
    225                 getAuthSchemes());
    226         context.setAttribute(
    227                 ClientContext.COOKIESPEC_REGISTRY,
    228                 getCookieSpecs());
    229         context.setAttribute(
    230                 ClientContext.COOKIE_STORE,
    231                 getCookieStore());
    232         context.setAttribute(
    233                 ClientContext.CREDS_PROVIDER,
    234                 getCredentialsProvider());
    235         return context;
    236     }
    237 
    238 
    239     @Override
    240     protected ConnectionReuseStrategy createConnectionReuseStrategy() {
    241         return new DefaultConnectionReuseStrategy();
    242     }
    243 
    244     @Override
    245     protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() {
    246         return new DefaultConnectionKeepAliveStrategy();
    247     }
    248 
    249 
    250     @Override
    251     protected AuthSchemeRegistry createAuthSchemeRegistry() {
    252         AuthSchemeRegistry registry = new AuthSchemeRegistry();
    253         registry.register(
    254                 AuthPolicy.BASIC,
    255                 new BasicSchemeFactory());
    256         registry.register(
    257                 AuthPolicy.DIGEST,
    258                 new DigestSchemeFactory());
    259         return registry;
    260     }
    261 
    262 
    263     @Override
    264     protected CookieSpecRegistry createCookieSpecRegistry() {
    265         CookieSpecRegistry registry = new CookieSpecRegistry();
    266         registry.register(
    267                 CookiePolicy.BEST_MATCH,
    268                 new BestMatchSpecFactory());
    269         registry.register(
    270                 CookiePolicy.BROWSER_COMPATIBILITY,
    271                 new BrowserCompatSpecFactory());
    272         registry.register(
    273                 CookiePolicy.NETSCAPE,
    274                 new NetscapeDraftSpecFactory());
    275         registry.register(
    276                 CookiePolicy.RFC_2109,
    277                 new RFC2109SpecFactory());
    278         registry.register(
    279                 CookiePolicy.RFC_2965,
    280                 new RFC2965SpecFactory());
    281         return registry;
    282     }
    283 
    284 
    285     @Override
    286     protected BasicHttpProcessor createHttpProcessor() {
    287         BasicHttpProcessor httpproc = new BasicHttpProcessor();
    288         httpproc.addInterceptor(new RequestDefaultHeaders());
    289         // Required protocol interceptors
    290         httpproc.addInterceptor(new RequestContent());
    291         httpproc.addInterceptor(new RequestTargetHost());
    292         // Recommended protocol interceptors
    293         httpproc.addInterceptor(new RequestConnControl());
    294         httpproc.addInterceptor(new RequestUserAgent());
    295         httpproc.addInterceptor(new RequestExpectContinue());
    296         // HTTP state management interceptors
    297         httpproc.addInterceptor(new RequestAddCookies());
    298         httpproc.addInterceptor(new ResponseProcessCookies());
    299         // HTTP authentication interceptors
    300         httpproc.addInterceptor(new RequestTargetAuthentication());
    301         httpproc.addInterceptor(new RequestProxyAuthentication());
    302         return httpproc;
    303     }
    304 
    305 
    306     @Override
    307     protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
    308         return new DefaultHttpRequestRetryHandler();
    309     }
    310 
    311 
    312     @Override
    313     protected RedirectHandler createRedirectHandler() {
    314         return new DefaultRedirectHandler();
    315     }
    316 
    317 
    318     @Override
    319     protected AuthenticationHandler createTargetAuthenticationHandler() {
    320         return new DefaultTargetAuthenticationHandler();
    321     }
    322 
    323 
    324     @Override
    325     protected AuthenticationHandler createProxyAuthenticationHandler() {
    326         return new DefaultProxyAuthenticationHandler();
    327     }
    328 
    329 
    330     @Override
    331     protected CookieStore createCookieStore() {
    332         return new BasicCookieStore();
    333     }
    334 
    335 
    336     @Override
    337     protected CredentialsProvider createCredentialsProvider() {
    338         return new BasicCredentialsProvider();
    339     }
    340 
    341 
    342     @Override
    343     protected HttpRoutePlanner createHttpRoutePlanner() {
    344         // BEGIN android-changed
    345         //     Use the proxy specified by system properties
    346         return new ProxySelectorRoutePlanner(getConnectionManager().getSchemeRegistry(), null);
    347         // END android-changed
    348     }
    349 
    350 
    351     @Override
    352     protected UserTokenHandler createUserTokenHandler() {
    353         return new DefaultUserTokenHandler();
    354     }
    355 
    356 } // class DefaultHttpClient
    357