Search code examples
springresthttpclient

why does it make a different response code between using httpclient and java.net?


I am trying to request to server by using HttpClient and java.net. However, each of them makes different response code. Here's the log. First one is the result by using HttpClient and Second one is using java.net.


Solution

  • There could be a number of reasons why the response is different, being the same URL and POST payload, like different headers being used by default. When debugging these kind of things, try to print the whole request and response pair as there is more to it than just the url. Still, for your case, the 302 means the resource was found but should be requested somewhere else.

    The tricky part here is that the client should not follow the redirect unless you tell it to do so.

    If you've not inadvertently set it up that way, then java.net is doing the wrong thing, even though you'd want it to follow the redirect 99.99% of the time.