Apache Httpclient 4.5 Ssl Example

Posted on  by admin

Using Secure Socket Layer, you can establish a secured connection between the client and server. It helps to safeguard sensitive information such as credit card numbers, usernames, passwords, pins, etc. You can make connections more secure by creating your own SSL context using the HttpClient library. Feb 08, 2008 Customizing SSL in HttpClient. The default behaviour of HttpClient is suitable for most uses, however there are some aspects which you may want to configure. The most common requirements for customizing SSL are: Ability to accept self-signed or untrusted SSL certificates.

Posted on the 13 February 2017 by Abhishek Somani @somaniabhi
Apache httpclient post example
This is a simple example on how to use Apache HttpClient 4.5 with connection pool manager to request https urls . In above code if you do not use setConnectionManagerShared(true) , you might get following error . Now if you want to use HttpClient to call Https Urls and with help of connection pools , you have to add a registry in your connection pool , otherwise it will ignore your SSLContext configurations , and give following error .

  • Apache HttpClient Tutorial
  • Apache HttpClient Resources
  • Selected Reading

Using HttpClient, we can perform Multipart upload, i.e., we can upload larger objects insmaller parts. In this chapter, we demonstrate the multipart upload in HTTP client by uploading a simple text file.

In general, any multipart upload contains three parts.

  • Initiation of the upload

  • Uploading the object parts

  • Completing the Multipart upload

For the multipart upload using HttpClient, we need to follow the below steps −

  • Create a multipart builder.

  • Add desired parts to it.

  • Complete the build and obtain a multipart HttpEntity.

  • Build request by setting the above muti-part entity.

  • Execute the request.

Following are the steps to upload a multipart entity using the HttpClient library.

Step 1 - Create an HttpClient object

The createDefault() method of the HttpClients class returns an object of the class CloseableHttpClient, which is the base implementation of the HttpClient interface. Using this method, create an HttpClient object −

Step 2 - Create a FileBody object

FileBody class represents the binary body part backed by a file. Instantiate this class by passing a File object and a ContentType object representing the type of the content.

Apache httpclient example

Step 3 - Create a MultipartEntityBuilder

The MultipartEntityBuilder class is used to build the multi-part HttpEntity object. Create its object using the create() method (of the same class).

Step 4 - Set the mode

A MultipartEntityBuilder has three modes: STRICT, RFC6532, and BROWSER_COMPATIBLE. Set it to the desired mode using the setMode() method.

Step 5 - Add various the desired parts

Using the methods addTextBody(), addPart() and, addBinaryBody(), you can add simple text, files, streams, and other objects to a MultipartBuilder. Add the desired contents using these methods.

Step 6 - Building single entity

You can build all these parts to a single entity using the build() method of the MultipartEntityBuilder class. Using this method, build all the parts into a single HttpEntity.

Step 7 - Create a RequestBuilder object

The class RequestBuilder is used to build request by adding parameters to it. If the request is of type PUT or POST, it adds the parameters to the request as URL encoded entity.

Create a RequestBuilder object (of type POST) using the post() method. And pass the Urito which you wanted to send the request it as a parameter.

Step 8 - Set the entity object to the RequestBuilder

Set the above created multipart entity to the RequestBuilder using the setEntity() method of the RequestBuilder class.

Step 9 - Build the HttpUriRequest

Apache Httpclient 4.5 Ssl Examples

Build a HttpUriRequest request object using the build() method of the RequestBuilder class.

Step 10 - Execute the request

Using the execute() method, execute the request built in the previous step (bypassing the request as a parameter to this method).


Apache httpclient 4.5 ssl example page

Following example demonstrates how to send a multipart request using the HttpClient library. In this example, we are trying to send a multipart request backed by a file.

Apache Httpclient


Apache Httpclient 4.5 Ssl Example Code


Apache Httpclient Post Example

On executing, the above program generates the following output −