简体   繁体   English

Java Http(s)URLConnection java.io.IOException:服务器返回 HTTP 响应代码:403

[英]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: 在某些部署中,由于以下原因,倾向于使用这种设置:

  • 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. 它允许将应用程序的静态服务元素复制到Apache HTTPD处理端,在该方面,Apache HTTPD在从磁盘提供静态文件方面通常比Tomcat快得多。
  • It permits one to forward only "valid" requests, reducing the load on Tomcat. 它只允许转发“有效”请求,从而减少了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 即使有人不小心部署了Tomcat的Web管理工具,它也不允许转发任何可能重新配置Tomcat中的应用程序部署或配置的请求。
  • It permits one to use the web management tool provided the requests to do so come from a trusted set of internet addresses. 它允许使用Web管理工具,前提是该请求来自受信任的一组Internet地址。

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.

相关问题 URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400 - URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL 在Java代码中获取“ java.io.IOException:服务器返回的HTTP响应代码:URL的403:” - Getting “java.io.IOException: Server returned HTTP response code: 403 for URL: ” in java code java.io.IOException:服务器在Java中返回了HTTP响应代码:URL的403: - java.io.IOException: Server returned HTTP response code: 403 for URL: in java java.io.IOException:服务器返回HTTP响应代码:502 - java.io.IOException: Server returned HTTP response code: 502 java.io.IOException:服务器返回HTTP响应代码:500 - java.io.IOException: Server returned HTTP response code: 500 java.io.IOException:服务器返回HTTP响应代码:403表示URL - java.io.IOException: Server returned HTTP response code: 403 for URL java.io.IOException:服务器返回HTTP响应代码:403,URL: - java.io.IOException: Server returned HTTP response code: 403 for URL: java.io.IOException:服务器返回HTTP响应代码:403,URL: <url> - java.io.IOException: Server returned HTTP response code: 403 for URL: <url> java.io.IOException:服务器返回的HTTP响应代码:URL的403 - java.io.IOException: Server returned HTTP response code: 403 for URL 错误:java.io.IOException:服务器返回 HTTP 响应代码:URL 403 - Error : java.io.IOException: Server returned HTTP response code: 403 for URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM