10

I am working on a project in which I need to make a call to my service and my service will return the data back in JSON format. And I don't need to serialize this JSON response to any POJO, I just need to get the data back as String. And this application is very performance critical so HttpClient has to be pretty fast

So I decided to use Apache HttpClient or is there any better alternative which I can use?

DefaultHttpClient httpclient = new DefaultHttpClient(); HttpUriRequest request = new HttpGet("some-url"); request.addHeader("Context", "some-value"); HttpResponse response = httpClient.execute(request); String response = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); 

But it complains that The type DefaultHttpClient is deprecated so maybe they have new version of HttpClient or some other way of making a HttpClient call to an URL?

Is there anything I am missing?

1

2 Answers 2

18

Use this:

HttpClient httpclient = HttpClientBuilder.create().build(); 

Refer to this stackoverflow post

Sign up to request clarification or add additional context in comments.

Comments

6

From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated.

Use following maven dependency as an example.

<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version> </dependency> 

Following import.

import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGett; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.client.methods.HttpUriRequest; 

Following code block.

HttpClient client = HttpClientBuilder.create().build(); HttpUriRequest httpUriRequest = new HttpGet("http://example.domain/someuri"); HttpResponse response = client.execute(httpUriRequest); System.out.println("Response:"+response); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.