How to get headers? (java,httpclient 4.X)

后端 未结 1 1973
梦如初夏
梦如初夏 2021-01-12 00:19

When I do:

Header[] h = first.getAllHeaders();

The returned Header array is empty. Any ideas? Below is my code.


相关标签:
1条回答
  • 2021-01-12 01:05

    You're calling getAllHeaders() on first, which is your HttpGet object. You want to call getAllHeaders() on the response object like this:

    Header[] h = response.getAllHeaders();
    

    You can also check the Response's status code and respond accordingly like this:

    int statusCode = response.getStatusLine().getStatusCode();
    Logger.d("Response returned status code " + statusCode);
    
    if (HttpStatus.SC_OK == statusCode) {
        // TODO: handle 200 OK
    } else if (HttpStatus.SC_NOT_FOUND == statusCode) { 
        // TODO: handle 404 Not Found
    } else { 
        // TODO: handle other codes here
    }
    
    0 讨论(0)
提交回复
热议问题