Bluefire
Bluefire

Reputation: 14099

Java HTTP GET request gives 403 Forbidden, but works in browser

I have some code, which is meant to send a GET request via HTTP to a server, and fetch the data there. I haven't yet coded the part that does stuff with the response, as I first wanted to test whether the GET request worked. And it didn't:

private static String fetch() throws UnsupportedEncodingException, MalformedURLException, IOException {
        // Set the parameters
        String url = "http://www.futhead.com";
        String charset = "UTF-8";

        //Fire the request
        try {
        URLConnection connection = new URL(url).openConnection();
        connection.setRequestProperty("Accept-Charset", charset);
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        // ^^^ I tried this, and it doesn't help!
        InputStream response = connection.getInputStream();

        HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
        httpConnection.setRequestMethod("GET");
        System.out.println("Status: " + httpConnection.getResponseCode());
        } catch (UnknownHostException e) {
            // stuff
        }
        return null;
        // ^^^ I haven't coded the fetching itself yet
    }

With that code in mind, fetch() prints Status: 403. Why is this happening? My guess is that this particular server doesn't let non-browser clients access it (because the code works with http://www.google.com), but is there a workaround?

There are some answers out there already, but some of them are either irrelevant to me (they talk about a problem with HTTPS) or incomprehensible. I've tried those that I can understand, to no avail.

Upvotes: 2

Views: 3981

Answers (2)

Martin
Martin

Reputation: 1

I had the same problem.

I solved it by removing the java startparameter: -Djsse.enableSNIExtension=false

Upvotes: 0

rvange
rvange

Reputation: 2572

You might have Browser Integrity Check enabled https://support.cloudflare.com/hc/en-us/articles/200170086-What-does-the-Browser-Integrity-Check-do-

I disabled Browser Integrity Check and it works fine now. Another solution would be to set User-Agent, if possible.

I experienced the problem from Scala, which eventually uses java.net.URL

Upvotes: 3

Related Questions