1

java.io.IOException: Server returned HTTP response code: 403 for http://www.budgetbottle.com/vivino.xml

This URL is working (returns the contents) when running by Java class main method an at local tomcat server but it is throwing error when running on the tomcat server on production machine.

The code as follows:

URL url = new URL("http://www.budgetbottle.com/vivino.xml"); HttpURLConnection urlcon = (HttpURLConnection) url.openConnection(); urlcon.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); // System.setProperty("http.agent", ""); System.setProperty("http.agent", "Chrome"); // urlcons.setRequestProperty("Content-Language", "en-US"); // urlcons.setUseCaches(false); // urlcons.setDoInput(true); // urlcons.setDoOutput(true); 

I have already applied the solutions provided on the similar questions asked on this forum but no solution works.

Update:
I just noticed, the wget utility also throws the same error at the production system while no issue at the local system. Please note the options like User-Agent are used

Does it a system specific error?

2

3 Answers 3

2

I used the code below and it worked fine for me. Do you have some kind of ratelimit or IP ban system on the server, that could be blocking your connection?

 URL url = new URL("http://www.budgetbottle.com/vivino.xml"); HttpURLConnection urlcon = (HttpURLConnection) url.openConnection(); urlcon.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); System.setProperty("http.agent", "Chrome"); BufferedReader reader = new BufferedReader(new InputStreamReader(urlcon.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } System.out.println(sb.toString()); 
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your code which is working perfectly at my local system but, it is failing at the production system. FYI, the Java version is same at both system
Could I have the stacktrace and the error message that is thrown? Also if you could provide log for that request on your production server?
Here is the stack trace: java.io.IOException: Server returned HTTP response code: 403 for URL: budgetbottle.com/vivino.xml at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627) at com.batch.Spider.main(Spider.java:2041)
Have you tried different User-Agents, because only way that I'm getting 403 is with no User-Agent. Also I'm using java.net.HttpURLConnection instead of sun.new.www.protocol.http.HttpURLConnection.
0

It is quite possible that the production server is running behind an Apache HTTPD server that is proxying the requests to a Tomcat Server. This kind of setup is favored in some deployments for the following reasons:

  • It permits one to copy the staticly served elements of an application into the Apache HTTPD handling side, where Apache HTTPD is generally much faster than Tomcat in serving up static files from disk.
  • It permits one to forward only "valid" requests, reducing the load on Tomcat.
  • It permits one to not forward any request that might reconfigure the application deployments or configuration within Tomcat, even if someone accidentally deploys Tomcat's web management tools
  • It permits one to use the web management tool provided the requests to do so come from a trusted set of internet addresses.

And there's a lot of additional items that might be of interest to a system admin, prompting them to deploy Tomcat behind Apache HTTPD.

In at least one scenario, it might also permit Apache HTTPD doing a challenge and response authentication in the HTTPD layer before forwarding the request to Tomcat.

You need to get a better understanding of your production deployment before you can proceed. Then you need to attempt to recreate it more accurately in your development environment.

1 Comment

Thanks @edwin, I too has this approach in my to do list. I am trying to set up a new machine with same configuration.
0

Could it be that your production server has somehow restricted network connectivity (all http/https connection attempts redirected to a captive proxy returning 'access denied')?

Are you able to reach any other external site from your production environment?

1 Comment

Thanks @juha-laiho for your suggestion. And yes, I am can reach to a external website from my production environment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.