How to get HttpClient returning status code and response body?

后端 未结 5 2092
谎友^
谎友^ 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: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());
    

提交回复
热议问题