1

When url "https://www.parcelhero.com" is used then the code gives the http response code as 403 despite the site opening successfully and giving the actual response code as 200.Please tell me the reason.

  try {
                    URL url = new URL("https://www.parcelhero.com");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();

            connection.setRequestMethod("GET");

                connection.connect();

            int code = connection.getResponseCode();
                   //e.printStackTrace();
                   System.out.println(code);
                   }
                catch (IOException e) {

            e.printStackTrace();
        }
user2044296
  • 504
  • 1
  • 7
  • 18
  • Did you try using the `HttpsURLConnection` class instead of `HttpURLConnection`? http://stackoverflow.com/questions/9960998/using-httpurlconnection-and-httpsurlconnection-to-connect-to-an-https – dnapierata Jul 10 '15 at 20:20
  • Yes, it's also not giving the expected code. – user2044296 Jul 10 '15 at 20:52

1 Answers1

1

Some website servers require certain HTTP Headers to be set when making a request. Otherwise, they will deny the request with a 403 response as you are getting.

You just need to set the User-Agent property of the request header by using connection.setRequestProperty() for it to work:

        try {
            URL url = new URL("https://www.parcelhero.com");
            HttpsURLConnection connection = (HttpsURLConnection)   
            url.openConnection();
            connection.setRequestMethod("GET");
            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");
            connection.connect();
            int code = connection.getResponseCode();
            //e.printStackTrace();
            System.out.println(code);
        }
        catch (IOException e) {
            e.printStackTrace(System.out);
        }

And here is a working code snippet for testing online: http://rextester.com/ITB98285

dnapierata
  • 1,153
  • 1
  • 16
  • 28
  • Thanks. But the code which I had given was working for other websites like google so is there any specific reason for it, I would really appreciate your response on this. – user2044296 Jul 11 '15 at 12:07
  • I've updated my answer to include the reason why this might happen. – dnapierata Jul 11 '15 at 17:46
  • Thanks a lot @dnapierata for being very helpful and thoughtful in resolving my query. – user2044296 Jul 11 '15 at 19:40
  • Can there be more user agents than the one mentioned by you and is Mozilla not sufficient for it? – user2044296 Jul 11 '15 at 20:26
  • @user2044296 There are plenty of user-agents and without actually seeing the configuration of the server it is impossible to tell which one's it might accept/reject.However, I think the key here is that the server just expects `User-Agent` of the request header to be set to **something**. Here is a similar post to the problem you are experiencing http://stackoverflow.com/questions/13670692/403-forbidden-with-java-but-not-web-browser . Also, if this answer was sufficient in solving your problem, marking it accepted would be much appreciated. – dnapierata Jul 13 '15 at 16:34
  • But today when I used the same code for https://www.cricinfo.com it gave "javax.net.ssl.sslhandshakeexception java.security.cert.certificateexception no subject alternative names present" error. So is this snippet of code not meant for universal use? Also this code fails to give 301 response code for the URLs where redirection takes place. – user2044296 Jul 13 '15 at 19:38
  • 1
    No, this snippet was not meant to be universal across all websites. It was just a solution to the question you originally asked. For the current error you were getting I would try looking here http://stackoverflow.com/a/19542614/1741244 . And StackOverflow is not a software development company so please make sure your question is **on-topic** http://stackoverflow.com/help/on-topic – dnapierata Jul 14 '15 at 16:05
  • I got it but there was nothing in it to get so offended. – user2044296 Jul 14 '15 at 19:01