HttpClient, okhttp, differences Jodd-http use

http request as the most common way network, for exchanging data, different http client, there are differences in performance use herein will HttpClient, okhttp, Jodd-http three put, post request method to make a comparison.

 1         <dependency>
 2             <groupId>org.jodd</groupId>
 3             <artifactId>jodd-http</artifactId>
 4             <version>5.1.4</version>
 5         </dependency>
 6 
 7         <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
 8         <dependency>
 9             <groupId>com.squareup.okhttp3</groupId>
10             <artifactId>okhttp</artifactId>
11             <version>4.4.0</version>
12         </dependency>
13 
14         <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
15         <dependency>
16             <groupId>org.apache.httpcomponents</groupId>
17             <artifactId>httpclient</artifactId>
18             <version>4.5.12</version>
19         </dependency>
20 
21         <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
22         <dependency>
23             <groupId>com.alibaba</groupId>
24             <artifactId>fastjson</artifactId>
25             <version>1.2.62</version>
26         </dependency>
27         
Required dependencies

Apache HttpClient

  Sending a request in the following steps:

  1. Creating CloseableHttpClient Object / CloseableHttpAsyncClient objects, the former synchronous, asynchronous latter
  2. Http request to create concrete objects, e.g. HttpGet, HttpPost
  3. Call the execute method execute the request, if it is an asynchronous request to be called the start method before executing

get request

    @Test
     public  void testApacheHttpGet (String URL) throws IOException {
         // set timeout 
        RequestConfig config = RequestConfig.custom () 
                .setConnectTimeout ( 60 * 1000)        // connection time 
                .setSocketTimeout (60 * 1000)      // acquisition response from the server timeout data 
                .build (); 
        CloseableHttpClient Client = HttpClientBuilder.create () Build ();. 
        HttpGet HttpGet = new new HttpGet (URL); 
        httpGet.setConfig (config); 
        CloseableHttpResponse Response = client.execute(httpGet);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

post request:

    @Test
    public void testApacheHttpPost(String url) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        httpPost.setEntity(new StringEntity(new JSONObject().toString()));  //设置请求体
        CloseableHttpResponse response = client.execute(httpPost);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

okhttp

  Sending a request in the following steps:

  1. Objects created OkHttpClient
  2. Create a Request object
  3. The Call Request object encapsulates
  4. Synchronous or asynchronous request is performed by Call, execute method is called synchronous execution, calls an asynchronous method performed enqueue

get request:

    @Test
    public void testOkHttpGet(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        System.out.println(response.body().toString());
    }

post request:

    @Test
    public void testOkHttpPost(String url) throws IOException {
        JSONObject json = new JSONObject();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),String.valueOf(json));
        Request build = new Request.Builder()
                .url(url)
                .post(body)
                .header("Content-Type","application/json;charset=utf8") .build(); Call call = okHttpClient.newCall(build); Response response = call.execute(); String string = response.body().string(); }

Set timeout

        = New new Client OkHttpClient OkHttpClient.Builder () 
                .connectTimeout (60, TimeUnit.SECONDS) // set connection time 
                .readTimeout (60, TimeUnit.SECONDS) // set the read timeout 
                .build ();

Set of client timeout, means that all requests will be taken this way out settings.

Jodd-http

  Jodd provide a lightweight, native http client, very simple to use and convenient.

get request:

 Request parameters may be directly spliced ​​behind url, a method may be specified by a query ()

    @Test
     public  void testJoddHttpGet (String URL) throws IOException { 
        the HttpResponse Response = the HttpRequest 
                .get (URL)        // assignment request mode 
                .contentType ( "file application / JSON")   // specify the encoding 
                .query ( "xxx", "xxx " ) 
                .connectionKeepAlive ( to true ) // long connection  
                .timeout (60 * 1000)   // timeout setting 
                .send ();         // send a request 
        System.out.println (response.bodyText ()); 
    }

  storing response data returned by the server. Examples of the response can be extracted from a variety of attributes, such as statusCode()orstatusPhrase()。

  response read response body in three ways:

        response.bodyText () body text to the specified header information encoded 
        response.bodyBytes () body bytes 
        response.body () to ISO-8859-1 encoding

post request:

  BodyText stored in the request body. We can also add parameters form ( "xx", "xx") Form

    public void testJoddHttpPost(String url) throws IOException {
        HttpResponse response = HttpRequest
                .post(url)
                .contentType("application/json")
                .bodyText("xxx")
                .send();
        System.out.println(response.bodyText());
    }

summary:

  • In terms of the code amount jodd-http <okhttp <apache-http.
  • Time-out settings, jodd-http and apache-httpclient more flexible, okhttp on the client set up, can not request a single set.

Guess you like

Origin www.cnblogs.com/jiezai/p/12512600.html