From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated.
HttpClient is fully thread-safe when used with a thread-safe connection manager such as MultiThreadedHttpConnectionManager.
If you are processing HTTP responses manually instead of using a response handler, you need to close all the http connections by yourself.
If we do not want to add any external libraries, Java's native HTTPClient is the first choice for Java 11+ applications. Spring WebClient is the preferred choice for Spring Boot applications more importantly if we are using reactive APIs.
Relevant imports:
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
Usage:
HttpClient httpClient = HttpClientBuilder.create().build();
EDIT (after Jules' suggestion):
As the build()
method returns a CloseableHttpClient
which is-a AutoClosable
, you can place the declaration in a try-with-resources statement (Java 7+):
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
// use httpClient (no need to close it explicitly)
} catch (IOException e) {
// handle
}
IMHO the accepted answer is correct but misses some 'teaching' as it does not explain how to come up with the answer. For all deprecated classes look at the JavaDoc (if you do not have it either download it or go online), it will hint at which class to use to replace the old code. Of course it will not tell you everything, but this is a start. Example:
...
*
* @deprecated (4.3) use {@link HttpClientBuilder}. <----- THE HINT IS HERE !
*/
@ThreadSafe
@Deprecated
public class DefaultHttpClient extends AbstractHttpClient {
Now you have the class to use, HttpClientBuilder
, as there is no constructor to get a builder instance you may guess that there must be a static method instead: create
. Once you have the builder you can also guess that as for most builders there is a build method, thus:
org.apache.http.impl.client.HttpClientBuilder.create().build();
AutoClosable:
As Jules hinted in the comments, the returned class implements java.io.Closable
, so if you use Java 7 or above you can now do:
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {...}
The advantage is that you do not have to deal with finally and nulls.
Other relevant info
Also make sure to read about connection pooling and set the timeouts.
Examples from Apache use this:
CloseableHttpClient httpclient = HttpClients.createDefault();
The class org.apache.http.impl.client.HttpClients
is there since version 4.3.
The code for HttpClients.createDefault()
is the same as the accepted answer in here.
Try jcabi-http
, which is a fluent Java HTTP client, for example:
String html = new JdkRequest("https://www.google.com")
.header(HttpHeaders.ACCEPT, MediaType.TEXT_HTML)
.fetch()
.as(HttpResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK)
.body();
Check also this blog post: http://www.yegor256.com/2014/04/11/jcabi-http-intro.html
It got deprecated in version 4.3-alpha1
which you use because of the LATEST
version specification. If you take a look at the javadoc of the class, it tells you what to use instead: HttpClientBuilder
.
In the latest stable version (4.2.3
) the DefaultHttpClient
is not deprecated yet.
I would suggest using the below method if you are trying to read the json data only.
URL requestUrl=new URL(url);
URLConnection con = requestUrl.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb=new StringBuilder();
int cp;
try {
while((cp=rd.read())!=-1){
sb.append((char)cp);
}
catch(Exception e){
}
String json=sb.toString();
Use HttpClientBuilder to build the HttpClient instead of using DefaultHttpClient
ex:
MinimalHttpClient httpclient = new HttpClientBuilder().build();
// Prepare a request object
HttpGet httpget = new HttpGet("http://www.apache.org/");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With