When I perform an HTTP request for
When I perform an HTTP request for
Do you know what the 301 code is? Not exactly an error, but the permanent redirect indicator code . The server is advising the HTTP client that the content has been moved to another URL. When the server sends this response, it probably also sent an HTTP header called Location
that contains the new URL. Your browser is reading this header and doing the redirect automatically. Your HTTP client in Java does not.
I took a look at your case and saw that what is happening is that it is redirecting to the HTTPS page. In that case, a simple solution is simply to exchange your URL for the HTTPS version.
In a more general case, you need to extract the URL manually and start another request. I'll give you a basic overview of how to do this to make it a bit more complete:
Function indicating whether redirection should occur:
public boolean devemosRedirecionar(int codigoDeRespostaHTTP) {
return codigoDeRespostaHTTP == HttpURLConnection.HTTP_MOVED_TEMP
|| codigoDeRespostaHTTP == HttpURLConnection.HTTP_MOVED_PERM
|| codigoDeRespostaHTTP == HttpURLConnection.HTTP_SEE_OTHER
}
How to extract the URL to use for redirection:
public String obterURLDeRedirecionamento(HttpUrlConnection conexao) {
return conexao.getHeaderField("Location");
}
With this data, simply reassemble your request and send again.