neo-_-man
neo-_-man

Reputation: 35

Jsoup: error 307 when trying to access a page

I'm trying to access the page http://www.betbrain.com with jsoup, but this give me error 307. Anyone knows how I can fix this?

String sURL = "http://www.betbrain.com";
Connection.Response res = Jsoup.connect(sURL).timeout(5000).ignoreHttpErrors(true).followRedirects(true).execute();

Upvotes: 1

Views: 1975

Answers (1)

Darwind
Darwind

Reputation: 7371

HTTP status code 307 is not an error, it's an information saying that the server is making a temporary redirect to another page.

See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for info about HTTP Status codes.

The response returned from your request holds the value for the redirect inside the headers.

To get the header-values you could something like this:

String[] headerValues = res.headers().values().toArray(new String[res.headers().values().size()]);
String[] headerKeys = res.headers().keySet().toArray(new String[res.headers().keySet().size()]);

for (int i = 0; i < headerValues.length; i++) {
    System.out.println("Key: " + headerKeys[i] + " - value: " + headerValues[i]);
}

You need you own code of course for this, as you need your response.

Now when you look at the headers written to the console you will see a key:

Location which has a value of http://www.betbrain.com/?attempt=1.

This is your URL to redirect to, so you would do something like:

String newRedirectedUrl = res.headers("location");
Connection.Response newResponse = Jsoup.connect(newRedirectUrl).execute();
// Parse the response accordingly.

I am not sure why jsoup isn't following this redirect correctly, but it seems like it could have something to do with the standard Java implementation of HTTP redirects.

Upvotes: 1

Related Questions