[英]Java Http(s)URLConnection java.io.IOException: Server returned HTTP response code: 403
java.io.IOException: Server returned HTTP response code: 403 for http://www.budgetbottle.com/vivino.xml java.io.IOException:服务器返回 HTTP 响应代码: http : //www.budgetbottle.com/vivino.xml 的403
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.此 URL 在本地 tomcat 服务器上通过 Java 类 main 方法运行时有效(返回内容),但在生产机器上的 tomcat 服务器上运行时抛出错误。
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.我刚刚注意到, wget实用程序也会在生产系统上引发相同的错误,而在本地系统上则没有问题。 Please note the options like User-Agent are used
请注意使用了User-Agent等选项
Does it a system specific error?它是系统特定的错误吗?
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?
服务器上是否有某种速率限制或IP禁止系统,可能会阻止您的连接?
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());
It is quite possible that the production server is running behind an Apache HTTPD server that is proxying the requests to a Tomcat Server. 生产服务器很可能在将请求代理到Tomcat服务器的Apache HTTPD服务器后面运行。 This kind of setup is favored in some deployments for the following reasons:
在某些部署中,由于以下原因,倾向于使用这种设置:
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. 而且,系统管理员可能还会感兴趣许多其他项目,促使他们在Apache HTTPD之后部署Tomcat。
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. 在至少一种情况下,它还可能允许Apache HTTPD在将请求转发到Tomcat之前在HTTPD层中进行质询和响应身份验证。
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.
然后,您需要尝试在开发环境中更准确地重新创建它。
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')? 可能是您的生产服务器某种程度上限制了网络连接(所有http / https连接尝试都重定向到返回“拒绝访问”的强制代理)?
Are you able to reach any other external site from your production environment? 您是否可以从生产环境访问任何其他外部站点?
exactly the same thing happened to me!完全一样的事情发生在我身上! How did you solve this issue?
你是如何解决这个问题的?
Thanks.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.