What is the difference between the various available HttpClients?

I am trying to write a simple HttpClient program. This is the first time I work with HttpClient , I am pretty confused about which banks to include.

I included apache-httpcomponents-httpclient.jar and org.apache.commons.httpclient.jar with these, when I create an HttpClient object, I see different methods in the client object

 package com.comverse.rht; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.URI; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; public class HttpClientTest { public static void main(String[] args) throws URIException { URI url = new URI("http://www.google.com/search?q=httpClient"); HttpClient client = new HttpClient(); GetMethod get = new GetMethod(); PostMethod post = new PostMethod(); String responseString; StringBuilder sb = new StringBuilder(); String line; // add request header get.setURI(url); get.addRequestHeader("User-Agent", "shaiksha429"); try { int respCode = client.executeMethod(get); System.out.println("Response Code:" +respCode); System.out.println( "PCRF HTTP Status" + HttpStatus.getStatusText(respCode) ); responseString = get.getResponseBodyAsString(); BufferedReader rd = null; rd = new BufferedReader( new InputStreamReader(get.getResponseBodyAsStream()) ); while ((line = rd.readLine()) != null) { sb.append(line + '\n'); } System.out.println(sb); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

But when I google, I see another example, as shown below. What is the difference between the two? Why HttpClient one HttpClient have run, and the other has executeMethod . Which one do I need to use?

 String url = "http://www.google.com/search?q=httpClient"; HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); // add request header request.addHeader("User-Agent", USER_AGENT); HttpResponse response = client.execute(request); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()) ); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } 
+5
source share
2 answers

From HttpClient version 3 to version there have been many changes. The second example is definitely from HttpClient 4, so the first example is probably from the previous version.

Here is the code that will do your Google search, and read the result in line

 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(60); connectionManager.setDefaultMaxPerRoute(6); try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(connectionManager).build()) { HttpGet request = new HttpGet("http://www.google.com/search?q=httpClient"); request.setHeader("User-Agent", "HttpClient"); try (CloseableHttpResponse response = client.execute(request)) { MediaType mediaType = MediaType.parseMediaType(response.getFirstHeader("Content-Type").getValue()); Charset charSet = mediaType.getCharSet(); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); String body = CharStreams.toString(new InputStreamReader(is, charSet)); System.out.println("body = " + body); EntityUtils.consume(entity); } } 

First, you probably want to create a connection pool so you can reuse the connection if you send multiple requests to the same server. A pool is usually created during application initialization, for example, as a Spring singleton bean.

I used ClosableHttpClient here because it works with load-try syntax and you need to close both HTTPClient and responseStream when you finish reading. HttpClient is actually a lightweight object, a state similar to a socket connection, and cookies are stored elsewhere.

I use Spring MediaType.parseMediaType () to get char encoding, and Guavas CharStreams to convert inputStream to String. In my case, google encoded content using latin-1 because the β€œsearch” is β€œsΓΈgning” in Danish.

The final step is to use EntityUtils.consume (entity) to ensure that all entity data has been read. If you use a connection pool, this is important because unread data will lead to disconnection and not reuse by the connection manager (this is very important if you use https).

+7
source

You are using a library whose interface has changed in major versions. You cannot accidentally copy jars and copy / paste examples without understanding which edition you are using and from which the example or snippet was released.

Take a look at the examples that accompany the latest issue and grab something old with salt.

Apache seems to be moving especially fast.

+2
source

Source: https://habr.com/ru/post/1260310/


All Articles