How can I get the URL of an HTTPResponse

19,034

Solution 1

You maybe be thinking of redirecting the client to a new url in which case you want to set Location not Locations

Requests have URLs, responses are just data packets sent back to the client.

Solution 2

Try this

for(Header header : response.getHeaders("Location")) {
  System.out.println("Location from connect:" + header.getValue());
}
Share:
19,034
Fabian
Author by

Fabian

Updated on June 04, 2022

Comments

  • Fabian
    Fabian almost 2 years

    how can I get the URL of an HTTPResponse? I tried:

    response.getHeaders("Locations")
    

    But I obtained:

    11-15 21:14:03.355: INFO/System.out(880): [Lorg.apache.http.Header;@43ea9568
    
  • Alex Jasmin
    Alex Jasmin over 13 years
    Assuming the Location header is what you want try: response.getLastHeader("Location").getValue()
  • Basic
    Basic over 13 years
    @Alexandre thanks for the edit but I did actually mean set - using the Location: header is a common way to redirect output.
  • Alex Jasmin
    Alex Jasmin over 13 years
    Sorry. I though it was a typo. But how does setting the Location header makes sense in this context? The OP is dealing with a response object from an http client library.
  • Basic
    Basic over 13 years
    @Alexandre The question itself didn't make sense as you noted in your comment on the Q - I was wondering if instead of mixing request/response, the OP had mixed get/set. The only reason I came to this conclusion is that the OP mentioned the Location: header which is included in responses for redirection. until the OP clarifies further, I don't think there's much more we can do to help
  • John Cowan
    John Cowan about 4 years
    Location:, in addition to its use with redirection, is the header used when your POST operation has created a new object with its own URL. This is part of the design of REST systems.