Apache Http Client



The Hyper-Text Transfer Protocol (HTTP) is perhaps the most significant protocol used on the Internet today. Web services, network-enabled appliances and the growth of network computing continue to expand the role of the HTTP protocol beyond user-driven web browsers, while increasing the number of applications that require HTTP support.

How to ignore SSL certificate (trust all) for Apache HttpClient 4.3? All the answers that I have found on SO treat previous versions, and the API changed. Related: How to ignore SSL certificate errors in Apache HttpClient 4.0; How to handle invalid SSL certificates with Apache HttpClient? In this quick article, we will discuss step by step how to use Apache HttpClient 4.5 to make an Http GET request. The HTTP GET method represents a representation of the specified resource. This could be as simple as getting an HTML page, or getting resources formatted in JSON, XML or etc. Requests using HTTP GET Request methods should be Idempotent, meaning: these should only retrieve data.

Although the java.net package provides basic functionality for accessing resources via HTTP, it doesn’t provide the full flexibility or functionality needed by many applications. HttpAsyncClient seeks to fill this void by providing an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations.

Designed for extension while providing robust support for the base HTTP protocol, HttpAsyncClient may be of interest to anyone building HTTP-aware client applications based on asynchronous, event driven I/O model.

Documentation

  1. Quick Start - contains a simple, complete example of asynchronous request execution.
  2. HttpAsyncClient Examples - a set of examples demonstrating some of the more complex use scenarios.
  3. Javadocs

Features

  • Standards based, pure Java, implementation of HTTP versions 1.0 and 1.1
  • Full implementation of all HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) in an extensible OO framework.
  • Supports encryption with HTTPS (HTTP over SSL) protocol.
  • Transparent connections through HTTP proxies.
  • Tunneled HTTPS connections through HTTP proxies, via the CONNECT method.
  • Connection management support concurrent request execution. Supports setting the maximum total connections as well as the maximum connections per host. Detects and closes expired connections.
  • Persistent connections using KeepAlive in HTTP/1.0 and persistence in HTTP/1.1
  • The ability to set connection timeouts.
  • Source code is freely available under the Apache License.
  • Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO and Kerberos authentication schemes.
  • Plug-in mechanism for custom authentication schemes.
  • Automatic Cookie handling for reading Set-Cookie: headers from the server and sending them back out in a Cookie header when appropriate.
  • Plug-in mechanism for custom cookie policies.
  • Support for HTTP/1.1 response caching.
  • Support for pipelined request execution and processing.

Standards Compliance

HttpAsyncClient strives to conform to the following specifications endorsed by the Internet Engineering Task Force (IETF) and the internet at large:

  • RFC 1945 Hypertext Transfer Protocol – HTTP/1.0
  • RFC 2616 Hypertext Transfer Protocol – HTTP/1.1
  • RFC 2617 HTTP Authentication: Basic and Digest Access Authentication
  • RFC 2109 HTTP State Management Mechanism (Cookies)
  • RFC 2965 HTTP State Management Mechanism (Cookies v2)
  • Apache HttpClient Tutorial
  • Apache HttpClient Resources
  • Selected Reading
Http
Client

A Proxy server is an intermediary server between the client and the internet. Proxy servers offer the following basic functionalities −

  • Firewall and network data filtering

  • Network connection sharing

  • Data caching

Using HttpClient library, you can send a HTTP request using a proxy. Follow the steps given below −

Step 1 - Create a HttpHost object

Instantiate the HttpHost class of the org.apache.http package by passing a string parameter representing the name of the proxy host, (from which you need the requests to be sent) to its constructor.

In the same way, create another HttpHost object to represent the target host to which requests need to be sent.

Step 2 - Create an HttpRoutePlanner object

The HttpRoutePlanner interface computes a route to a specified host. Create an object of this interface by instantiating the DefaultProxyRoutePlanner class, an implementation of this interface. As a parameter to its constructor, pass the above created proxy host −

Step 3 - Set the route planner to a client builder

Using the custom() method of the HttpClients class, create a HttpClientBuilder object and, to this object set the route planner created above, using the setRoutePlanner() method.

Apache Httpclient Example

Step 4 - Build the CloseableHttpClient object

Build the CloseableHttpClient object by calling the build() method.

Step 5 - Create a HttpGetobject

Create a HTTP GET request by instantiating the HttpGet class.

Step 6 - Execute the request

Apache Http Client Default Timeout

One of the variants of the execute() method accepts an HttpHost and HttpRequest objects and executes the request. Execute the request using this method −

Example

Following example demonstrates how to send a HTTP request to a server via proxy. In thisexample, we are sending a HTTP GET request to google.com via localhost. We have printed the headers of the response and the body of the response.

Apache Http Client Connection Pool

Output

Apache Httpclient Api

On executing, the above program generates the following output −