1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.http.impl.client; 29 30 import org.apache.http.HttpVersion; 31 import org.apache.http.annotation.Contract; 32 import org.apache.http.annotation.ThreadingBehavior; 33 import org.apache.http.client.protocol.RequestAddCookies; 34 import org.apache.http.client.protocol.RequestAuthCache; 35 import org.apache.http.client.protocol.RequestClientConnControl; 36 import org.apache.http.client.protocol.RequestDefaultHeaders; 37 import org.apache.http.client.protocol.RequestProxyAuthentication; 38 import org.apache.http.client.protocol.RequestTargetAuthentication; 39 import org.apache.http.client.protocol.ResponseProcessCookies; 40 import org.apache.http.conn.ClientConnectionManager; 41 import org.apache.http.params.HttpConnectionParams; 42 import org.apache.http.params.HttpParams; 43 import org.apache.http.params.HttpProtocolParams; 44 import org.apache.http.params.SyncBasicHttpParams; 45 import org.apache.http.protocol.BasicHttpProcessor; 46 import org.apache.http.protocol.HTTP; 47 import org.apache.http.protocol.RequestContent; 48 import org.apache.http.protocol.RequestExpectContinue; 49 import org.apache.http.protocol.RequestTargetHost; 50 import org.apache.http.protocol.RequestUserAgent; 51 import org.apache.http.util.VersionInfo; 52 53 /** 54 * Default implementation of {@link org.apache.http.client.HttpClient} pre-configured 55 * for most common use scenarios. 56 * <p> 57 * Please see the Javadoc for {@link #createHttpProcessor()} for the details of the interceptors 58 * that are set up by default. 59 * <p> 60 * Additional interceptors can be added as follows, but 61 * take care not to add the same interceptor more than once. 62 * <pre> 63 * DefaultHttpClient httpclient = new DefaultHttpClient(); 64 * httpclient.addRequestInterceptor(new RequestAcceptEncoding()); 65 * httpclient.addResponseInterceptor(new ResponseContentEncoding()); 66 * </pre> 67 * <p> 68 * This class sets up the following parameters if not explicitly set: 69 * <ul> 70 * <li>Version: HttpVersion.HTTP_1_1</li> 71 * <li>ContentCharset: HTTP.DEFAULT_CONTENT_CHARSET</li> 72 * <li>NoTcpDelay: true</li> 73 * <li>SocketBufferSize: 8192</li> 74 * <li>UserAgent: Apache-HttpClient/release (java 1.5)</li> 75 * </ul> 76 * <p> 77 * The following parameters can be used to customize the behavior of this 78 * class: 79 * <ul> 80 * <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}</li> 81 * <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li> 82 * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li> 83 * <li>{@link org.apache.http.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}</li> 84 * <li>{@link org.apache.http.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}</li> 85 * <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}</li> 86 * <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li> 87 * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li> 88 * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li> 89 * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li> 90 * <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li> 91 * <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li> 92 * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li> 93 * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li> 94 * <li>{@link org.apache.http.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li> 95 * <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li> 96 * <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li> 97 * <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li> 98 * <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li> 99 * <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li> 100 * <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li> 101 * <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY}</li> 102 * <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li> 103 * <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_REDIRECTS}</li> 104 * <li>{@link org.apache.http.client.params.ClientPNames#MAX_REDIRECTS}</li> 105 * <li>{@link org.apache.http.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li> 106 * <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}</li> 107 * <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}</li> 108 * <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}</li> 109 * <li>{@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}</li> 110 * </ul> 111 * 112 * @since 4.0 113 * 114 * @deprecated (4.3) use {@link HttpClientBuilder} see also {@link CloseableHttpClient}. 115 */ 116 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL) 117 @Deprecated 118 public class DefaultHttpClient extends AbstractHttpClient { 119 120 /** 121 * Creates a new HTTP client from parameters and a connection manager. 122 * 123 * @param params the parameters 124 * @param conman the connection manager 125 */ 126 public DefaultHttpClient( 127 final ClientConnectionManager conman, 128 final HttpParams params) { 129 super(conman, params); 130 } 131 132 133 /** 134 * @since 4.1 135 */ 136 public DefaultHttpClient( 137 final ClientConnectionManager conman) { 138 super(conman, null); 139 } 140 141 142 public DefaultHttpClient(final HttpParams params) { 143 super(null, params); 144 } 145 146 147 public DefaultHttpClient() { 148 super(null, null); 149 } 150 151 152 /** 153 * Creates the default set of HttpParams by invoking {@link DefaultHttpClient#setDefaultHttpParams(HttpParams)} 154 * 155 * @return a new instance of {@link SyncBasicHttpParams} with the defaults applied to it. 156 */ 157 @Override 158 protected HttpParams createHttpParams() { 159 final HttpParams params = new SyncBasicHttpParams(); 160 setDefaultHttpParams(params); 161 return params; 162 } 163 164 /** 165 * Saves the default set of HttpParams in the provided parameter. 166 * These are: 167 * <ul> 168 * <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}: 169 * 1.1</li> 170 * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_CONTENT_CHARSET}: 171 * ISO-8859-1</li> 172 * <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}: 173 * true</li> 174 * <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}: 175 * 8192</li> 176 * <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}: 177 * Apache-HttpClient (Java 1.5)</li> 178 * </ul> 179 */ 180 public static void setDefaultHttpParams(final HttpParams params) { 181 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 182 HttpProtocolParams.setContentCharset(params, HTTP.DEF_CONTENT_CHARSET.name()); 183 HttpConnectionParams.setTcpNoDelay(params, true); 184 HttpConnectionParams.setSocketBufferSize(params, 8192); 185 HttpProtocolParams.setUserAgent(params, VersionInfo.getUserAgent("Apache-HttpClient", 186 "org.apache.http.client", DefaultHttpClient.class)); 187 } 188 189 /** 190 * Create the processor with the following interceptors: 191 * <ul> 192 * <li>{@link RequestDefaultHeaders}</li> 193 * <li>{@link RequestContent}</li> 194 * <li>{@link RequestTargetHost}</li> 195 * <li>{@link RequestClientConnControl}</li> 196 * <li>{@link RequestUserAgent}</li> 197 * <li>{@link RequestExpectContinue}</li> 198 * <li>{@link RequestAddCookies}</li> 199 * <li>{@link ResponseProcessCookies}</li> 200 * <li>{@link RequestAuthCache}</li> 201 * <li>{@link RequestTargetAuthentication}</li> 202 * <li>{@link RequestProxyAuthentication}</li> 203 * </ul> 204 * <p> 205 * @return the processor with the added interceptors. 206 */ 207 @Override 208 protected BasicHttpProcessor createHttpProcessor() { 209 final BasicHttpProcessor httpproc = new BasicHttpProcessor(); 210 httpproc.addInterceptor(new RequestDefaultHeaders()); 211 // Required protocol interceptors 212 httpproc.addInterceptor(new RequestContent()); 213 httpproc.addInterceptor(new RequestTargetHost()); 214 // Recommended protocol interceptors 215 httpproc.addInterceptor(new RequestClientConnControl()); 216 httpproc.addInterceptor(new RequestUserAgent()); 217 httpproc.addInterceptor(new RequestExpectContinue()); 218 // HTTP state management interceptors 219 httpproc.addInterceptor(new RequestAddCookies()); 220 httpproc.addInterceptor(new ResponseProcessCookies()); 221 // HTTP authentication interceptors 222 httpproc.addInterceptor(new RequestAuthCache()); 223 httpproc.addInterceptor(new RequestTargetAuthentication()); 224 httpproc.addInterceptor(new RequestProxyAuthentication()); 225 return httpproc; 226 } 227 228 }