Apache Http Client 4
1. Overview
In this very quick tutorial, I will show how to get and validate the StatusCode of the HTTP Response using HttpClient 4.
In this quick article, we will discuss step by step how to use Apache HttpClient 4.5 to make an HTTP POST request. The HTTP POST request method requests that the server accepts the entity enclosed in the request as a new subordinate of the web resource identified by the URI. Packages; org.apache.http.auth: Client HTTP authentication APIs. Org.apache.http.auth.params: Deprecated. Org.apache.http.client: Client HTTP communication APIs.
If you want to dig deeper and learn other cool things you can do with the HttpClient – head on over to the main HttpClient tutorial.
2. Retrieve the Status Code from the Http Response
After sending the Http request – we get back an instance of org.apache.http.HttpResponse – which allows us to access the status line of the response, and implicitly the Status Code:
Using this, we can validate that the code we receive from the server is indeed correct:
Download endpoint security for mac. Notice that we're using the predefined Status Codes also available in the library via org.apache.http.HttpStatus.
Apache Httpclient 405
3. Conclusion
See you for mac. This very simple example shows how to retrieve and work with Status Codes with the Apache HttpClient 4.
Apache Httpclient Download
The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.