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.