1

I am getting 200 as a response code instead of 3xx when I perform HTTP post request, even though web page gets redirected automatically. I tried this, but dosen't work. My code:

HttpURLConnection con = getMultipartHttpURLConnection(
        formParameter.getServerUrl(), boundary);
setRequestHeaders(con);
String urlParameters = getParameter(formParameter.getForm());
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
responseCode = con.getResponseCode();
String header = con.getHeaderField("Location");

Edit:

By some research I find out that if server is configured to redirect to https:// enabled url, then I am getting 3xx as response code, otherwise not. Example given in the link above does the same.

1 Answer 1

1

That's because HttpURLConnection is smart and handles the redirection internally. The 200 response code is the response code of the new location to which the redirection happened.

The only way that I am aware of how you could retrieve the redirection URL is to use setFollowRedirects(false) and follow the redirection manually.

See the following example:

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetRedirectURL {
    public static void main(final String... args) throws IOException {
        final URL url = new URL("http://www.google.com/");
        //HttpURLConnection.setFollowRedirects(false);
        final HttpURLConnection con = (HttpURLConnection) url.openConnection();
        //con.setInstanceFollowRedirects(false);
        final int responseCode = con.getResponseCode();
        final String location = con.getHeaderField("Location");
        System.err.format("%d%n%s%n", responseCode, location);
    }
}

If you leave this code unmodified, it will print 200 null. If you uncomment any of the lines controlling the redirection behavior, the output changes to `200 http://www.google...' .

It's important that modifying the redirection settings is done before the statemachine of the HttpURLConnection instance is, by method calls that read from the response, to send the request.

Sign up to request clarification or add additional context in comments.

7 Comments

Ok,but how to read the redirection url and how to check whether redirection url exists or not.
See the edit that I just made, I forgot the original question because it wasn't repeated in the text.
i tried both connection.setInstanceFollowRedirects(true); HttpURLConnection.setFollowRedirects(true); and both dosen't helped. Please check the link that i edited in question.
Did you use true or false as value? It needs to be false, but in your comment you write true.
I tried using both true/false but that too dosen't make the difference.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.