How to get HttpClient returning status code and response body?

后端 未结 5 2080
谎友^
谎友^ 2020-11-29 08:03

I am trying to get Apache HttpClient to fire an HTTP request, and then display the HTTP response code (200, 404, 500, etc.) as well as the HTTP response body (text string).

相关标签:
5条回答
  • 2020-11-29 08:24

    Don't provide the handler to execute.

    Get the HttpResponse object, use the handler to get the body and get the status code from it directly

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
    
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            StatusLine statusLine = response.getStatusLine();
            System.out.println(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
            String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            System.out.println("Response body: " + responseBody);
        }
    }
    

    For quick single calls, the fluent API is useful:

    Response response = Request.Get(uri)
            .connectTimeout(MILLIS_ONE_SECOND)
            .socketTimeout(MILLIS_ONE_SECOND)
            .execute();
    HttpResponse httpResponse = response.returnResponse();
    StatusLine statusLine = httpResponse.getStatusLine();
    

    For older versions of java or httpcomponents, the code might look different.

    0 讨论(0)
  • 2020-11-29 08:27

    BasicResponseHandler throws if the status is not 2xx. See its javadoc.

    Here is how I would do it:

    HttpResponse response = client.execute( get );
    int code = response.getStatusLine().getStatusCode();
    InputStream body = response.getEntity().getContent();
    // Read the body stream
    

    Or you can also write a ResponseHandler starting from BasicResponseHandler source that don't throw when the status is not 2xx.

    0 讨论(0)
  • 2020-11-29 08:33

    Fluent facade API:

    Response response = Request.Get(uri)
            .connectTimeout(MILLIS_ONE_SECOND)
            .socketTimeout(MILLIS_ONE_SECOND)
            .execute();
    HttpResponse httpResponse = response.returnResponse();
    StatusLine statusLine = httpResponse.getStatusLine();
    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        // 响应内容编码自适应?(好像没那么智能)
        String responseContent = EntityUtils.toString(
                httpResponse.getEntity(), StandardCharsets.UTF_8.name());
    }
    
    0 讨论(0)
  • 2020-11-29 08:36

    If you are using Spring

    return new ResponseEntity<String>("your response", HttpStatus.ACCEPTED);
    
    0 讨论(0)
  • 2020-11-29 08:37

    You can avoid the BasicResponseHandler, but use the HttpResponse itself to get both status and response as a String.

    HttpResponse response = httpClient.execute(get);
    
    // Getting the status code.
    int statusCode = response.getStatusLine().getStatusCode();
    
    // Getting the response body.
    String responseBody = EntityUtils.toString(response.getEntity());
    
    0 讨论(0)
提交回复
热议问题