Whenever I try to get the status code of a particular URL, both JSoup and HttpConnection always returns 200 even if there is a 301/302 redirect for the request URL. Did anyone else face a similar issue and what should be the workaround to get the status code of the first request?
The general strategy if you want to see redirection status codes is to turn off automatic redirect following. If you then want to follow the redirects, you can do it yourself ... by hand ... after making a note of the status code.
The details will depend on what you are using; eg HttpUrlConnection, Apache HttpClient v3, Apache HttpClient v4, or something else.
HttpUrlConnection.setFollowRedirects(false)
- note that this a static
method that affects all future connection attempts. HttpMethod.setFollowRedirects(false)
. RedirectStrategy
, or disable redirect handling with HttpClientBuilder.disableRedirectHandling()
. Connection.followRedirects(false)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.