/* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package ch.boye.httpclientandroidlib.impl.client; import ch.boye.httpclientandroidlib.HttpVersion; import ch.boye.httpclientandroidlib.annotation.ThreadSafe; import ch.boye.httpclientandroidlib.client.HttpClient; import ch.boye.httpclientandroidlib.client.protocol.RequestAddCookies; import ch.boye.httpclientandroidlib.client.protocol.RequestAuthCache; import ch.boye.httpclientandroidlib.client.protocol.RequestClientConnControl; import ch.boye.httpclientandroidlib.client.protocol.RequestDefaultHeaders; import ch.boye.httpclientandroidlib.client.protocol.RequestProxyAuthentication; import ch.boye.httpclientandroidlib.client.protocol.RequestTargetAuthentication; import ch.boye.httpclientandroidlib.client.protocol.ResponseAuthCache; import ch.boye.httpclientandroidlib.client.protocol.ResponseProcessCookies; import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; import ch.boye.httpclientandroidlib.params.CoreConnectionPNames; import ch.boye.httpclientandroidlib.params.CoreProtocolPNames; import ch.boye.httpclientandroidlib.params.HttpConnectionParams; import ch.boye.httpclientandroidlib.params.HttpParams; import ch.boye.httpclientandroidlib.params.HttpProtocolParams; import ch.boye.httpclientandroidlib.params.SyncBasicHttpParams; import ch.boye.httpclientandroidlib.protocol.BasicHttpProcessor; import ch.boye.httpclientandroidlib.protocol.HTTP; import ch.boye.httpclientandroidlib.protocol.RequestContent; import ch.boye.httpclientandroidlib.protocol.RequestExpectContinue; import ch.boye.httpclientandroidlib.protocol.RequestTargetHost; import ch.boye.httpclientandroidlib.protocol.RequestUserAgent; import ch.boye.httpclientandroidlib.util.VersionInfo; /** * Default implementation of {@link HttpClient} pre-configured for most common use scenarios. *

* This class creates the following chain of protocol interceptors per default: *

*

* This class sets up the following parameters if not explicitly set: *

*

* The following parameters can be used to customize the behavior of this * class: *

* * @since 4.0 */ @ThreadSafe public class DefaultHttpClient extends AbstractHttpClient { /** * Creates a new HTTP client from parameters and a connection manager. * * @param params the parameters * @param conman the connection manager */ public DefaultHttpClient( final ClientConnectionManager conman, final HttpParams params) { super(conman, params); } /** * @since 4.1 */ public DefaultHttpClient( final ClientConnectionManager conman) { super(conman, null); } public DefaultHttpClient(final HttpParams params) { super(null, params); } public DefaultHttpClient() { super(null, null); } /** * Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)} * * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it. */ @Override protected HttpParams createHttpParams() { HttpParams params = new SyncBasicHttpParams(); setDefaultHttpParams(params); return params; } /** * Saves the default set of HttpParams in the provided parameter. * These are: * */ public static void setDefaultHttpParams(HttpParams params) { HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSocketBufferSize(params, 8192); // determine the release version from packaged version info final VersionInfo vi = VersionInfo.loadVersionInfo ("ch.boye.httpclientandroidlib.client", DefaultHttpClient.class.getClassLoader()); final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE; HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/" + release + " (java 1.5)"); } @Override protected BasicHttpProcessor createHttpProcessor() { BasicHttpProcessor httpproc = new BasicHttpProcessor(); httpproc.addInterceptor(new RequestDefaultHeaders()); // Required protocol interceptors httpproc.addInterceptor(new RequestContent()); httpproc.addInterceptor(new RequestTargetHost()); // Recommended protocol interceptors httpproc.addInterceptor(new RequestClientConnControl()); httpproc.addInterceptor(new RequestUserAgent()); httpproc.addInterceptor(new RequestExpectContinue()); // HTTP state management interceptors httpproc.addInterceptor(new RequestAddCookies()); httpproc.addInterceptor(new ResponseProcessCookies()); // HTTP authentication interceptors httpproc.addInterceptor(new RequestAuthCache()); httpproc.addInterceptor(new ResponseAuthCache()); httpproc.addInterceptor(new RequestTargetAuthentication()); httpproc.addInterceptor(new RequestProxyAuthentication()); return httpproc; } }