Converting from HttpClient 3 to 4

前端 未结 4 2170
情歌与酒
情歌与酒 2021-02-20 15:02

I\'ve managed to make changes to everything but the following:

HttpClient client;
HttpPost method;   
client = new DefaultHttpClient();
method = new HttpPost(url         


        
相关标签:
4条回答
  • 2021-02-20 15:32

    Use EntityUtils and check the returned entity to be not null before consuming the entity:

    InputStream rstream;
    try {
        HttpResponse response = client.execute(HttpHost, method);
    
        rstream = Optional.ofNullable(httpResponse.getEntity())
        .map(e -> response.getContent()).orElse(null);
    
    } catch (IOException e) {
        return BadSpot(e.getMessage()); 
    }
    

    NOTE: the InputStream here can be null and most of all you have to ensure that it's consumed before you actually close the response/release the connection.

    0 讨论(0)
  • 2021-02-20 15:42

    The util class has some helpful methods:

    EntityUtils.toString(response.getEntity());
    

    There is also some useful stuff in the examples at apache's website

    0 讨论(0)
  • 2021-02-20 15:43
    InputStream rstream;
    try {
        HttpResponse response = client.execute(HttpHost, method);
        rstream = response.getEntity().getContent();
    } catch (IOException e) {
        return BadSpot(e.getMessage()); 
    }
    

    above should do what you are asking.

    0 讨论(0)
  • 2021-02-20 15:53

    HttpResponse.getEntity(), followed by HttpEntity.getContent()

    0 讨论(0)
提交回复
热议问题