|
Java example source code file (DefaultHttpClient.java)
The DefaultHttpClient.java example source code/* * ==================================================================== * 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 * <http://www.apache.org/>. * */ package org.apache.http.impl.client; import org.apache.http.annotation.ThreadSafe; import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HttpVersion; import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; import org.apache.http.client.CookieStore; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.RedirectHandler; import org.apache.http.client.UserTokenHandler; import org.apache.http.client.params.AuthPolicy; import org.apache.http.client.params.ClientPNames; import org.apache.http.client.params.CookiePolicy; import org.apache.http.client.protocol.ClientContext; import org.apache.http.client.protocol.RequestAddCookies; import org.apache.http.client.protocol.RequestClientConnControl; import org.apache.http.client.protocol.RequestDefaultHeaders; import org.apache.http.client.protocol.RequestProxyAuthentication; import org.apache.http.client.protocol.RequestTargetAuthentication; import org.apache.http.client.protocol.ResponseProcessCookies; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionManagerFactory; import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.conn.routing.HttpRoutePlanner; 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.cookie.CookieSpecRegistry; import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.auth.BasicSchemeFactory; import org.apache.http.impl.auth.DigestSchemeFactory; import org.apache.http.impl.conn.DefaultHttpRoutePlanner; import org.apache.http.impl.conn.SingleClientConnManager; import org.apache.http.impl.cookie.BestMatchSpecFactory; import org.apache.http.impl.cookie.BrowserCompatSpecFactory; import org.apache.http.impl.cookie.NetscapeDraftSpecFactory; import org.apache.http.impl.cookie.RFC2109SpecFactory; import org.apache.http.impl.cookie.RFC2965SpecFactory; 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.BasicHttpProcessor; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestExecutor; import org.apache.http.protocol.RequestContent; import org.apache.http.protocol.RequestExpectContinue; import org.apache.http.protocol.RequestTargetHost; import org.apache.http.protocol.RequestUserAgent; import org.apache.http.util.VersionInfo; /** * Default implementation of {@link AbstractHttpClient}. * <p> * This class creates an instance of {@link SingleClientConnManager} * for connection management if not explicitly set. * <p> * This class creates the following chain of protocol interceptors per * default: * <ul> * <li>{@link RequestDefaultHeaders} * <li>{@link RequestContent} * <li>{@link RequestTargetHost} * <li>{@link RequestClientConnControl} * <li>{@link RequestUserAgent} * <li>{@link RequestExpectContinue} * <li>{@link RequestAddCookies} * <li>{@link ResponseProcessCookies} * <li>{@link RequestTargetAuthentication} * <li>{@link RequestProxyAuthentication} * </ul> * <p> * This class sets up the following parameters if not explicitly set: * <ul> * <li>Version: HttpVersion.HTTP_1_1 * <li>ContentCharset: HTTP.DEFAULT_CONTENT_CHARSET * <li>UseExpectContinue: true * <li>NoTcpDelay: true * <li>SocketBufferSize: 8192 * <li>UserAgent: Apache-HttpClient/release (java 1.5) * </ul> * <p> * The following parameters can be used to customize the behavior of this * class: * <ul> * <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION} * <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING} * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET} * <li>{@link org.apache.http.params.CoreProtocolPNames#USE_EXPECT_CONTINUE} * <li>{@link org.apache.http.params.CoreProtocolPNames#WAIT_FOR_CONTINUE} * <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT} * <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE} * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH} * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT} * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT} * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER} * <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY} * <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT} * <li>{@link org.apache.http.params.CoreConnectionPNames#STALE_CONNECTION_CHECK} * <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE} * <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS} * <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY} * <li>{@link org.apache.http.conn.params.ConnManagerPNames#TIMEOUT} * <li>{@link org.apache.http.conn.params.ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE} * <li>{@link org.apache.http.conn.params.ConnManagerPNames#MAX_TOTAL_CONNECTIONS} * <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS} * <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER} * <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET} * <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY} * <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION} * <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_REDIRECTS} * <li>{@link org.apache.http.client.params.ClientPNames#MAX_REDIRECTS} * <li>{@link org.apache.http.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS} * <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST} * <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST} * <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS} * <li>{@link org.apache.http.client.params.ClientPNames#CONNECTION_MANAGER_FACTORY_CLASS_NAME} * </ul> * * @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); } public DefaultHttpClient(final HttpParams params) { super(null, params); } public DefaultHttpClient() { super(null, null); } @Override protected HttpParams createHttpParams() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); HttpProtocolParams.setUseExpectContinue(params, true); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSocketBufferSize(params, 8192); // determine the release version from packaged version info final VersionInfo vi = VersionInfo.loadVersionInfo ("org.apache.http.client", getClass().getClassLoader()); final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE; HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/" + release + " (java 1.5)"); return params; } @Override protected HttpRequestExecutor createRequestExecutor() { return new HttpRequestExecutor(); } @Override protected ClientConnectionManager createClientConnectionManager() { SchemeRegistry registry = new SchemeRegistry(); registry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register( new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager connManager = null; HttpParams params = getParams(); ClientConnectionManagerFactory factory = null; String className = (String) params.getParameter( ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME); if (className != null) { try { Class<?> clazz = Class.forName(className); factory = (ClientConnectionManagerFactory) clazz.newInstance(); } catch (ClassNotFoundException ex) { throw new IllegalStateException("Invalid class name: " + className); } catch (IllegalAccessException ex) { throw new IllegalAccessError(ex.getMessage()); } catch (InstantiationException ex) { throw new InstantiationError(ex.getMessage()); } } if (factory != null) { connManager = factory.newInstance(params, registry); } else { connManager = new SingleClientConnManager(getParams(), registry); } return connManager; } @Override protected HttpContext createHttpContext() { HttpContext context = new BasicHttpContext(); context.setAttribute( ClientContext.SCHEME_REGISTRY, getConnectionManager().getSchemeRegistry()); context.setAttribute( ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute( ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute( ClientContext.COOKIE_STORE, getCookieStore()); context.setAttribute( ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; } @Override protected ConnectionReuseStrategy createConnectionReuseStrategy() { return new DefaultConnectionReuseStrategy(); } @Override protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() { return new DefaultConnectionKeepAliveStrategy(); } @Override protected AuthSchemeRegistry createAuthSchemeRegistry() { AuthSchemeRegistry registry = new AuthSchemeRegistry(); registry.register( AuthPolicy.BASIC, new BasicSchemeFactory()); registry.register( AuthPolicy.DIGEST, new DigestSchemeFactory()); return registry; } @Override protected CookieSpecRegistry createCookieSpecRegistry() { CookieSpecRegistry registry = new CookieSpecRegistry(); registry.register( CookiePolicy.BEST_MATCH, new BestMatchSpecFactory()); registry.register( CookiePolicy.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory()); registry.register( CookiePolicy.NETSCAPE, new NetscapeDraftSpecFactory()); registry.register( CookiePolicy.RFC_2109, new RFC2109SpecFactory()); registry.register( CookiePolicy.RFC_2965, new RFC2965SpecFactory()); return registry; } @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 RequestTargetAuthentication()); httpproc.addInterceptor(new RequestProxyAuthentication()); return httpproc; } @Override protected HttpRequestRetryHandler createHttpRequestRetryHandler() { return new DefaultHttpRequestRetryHandler(); } @Override protected RedirectHandler createRedirectHandler() { return new DefaultRedirectHandler(); } @Override protected AuthenticationHandler createTargetAuthenticationHandler() { return new DefaultTargetAuthenticationHandler(); } @Override protected AuthenticationHandler createProxyAuthenticationHandler() { return new DefaultProxyAuthenticationHandler(); } @Override protected CookieStore createCookieStore() { return new BasicCookieStore(); } @Override protected CredentialsProvider createCredentialsProvider() { return new BasicCredentialsProvider(); } @Override protected HttpRoutePlanner createHttpRoutePlanner() { return new DefaultHttpRoutePlanner (getConnectionManager().getSchemeRegistry()); } @Override protected UserTokenHandler createUserTokenHandler() { return new DefaultUserTokenHandler(); } } // class DefaultHttpClient Other Java examples (source code examples)Here is a short list of links related to this Java DefaultHttpClient.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.