Apache HttpClient 被抛出 HTTP 500,但无法在浏览器中重现 [英] Apache HttpClient is throwng HTTP 500s but can't reproduce in a browser

查看:29
本文介绍了Apache HttpClient 被抛出 HTTP 500,但无法在浏览器中重现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 HttpClient 4.1.3,我编写了以下代码:

Using HttpClient 4.1.3, I've written the following code:

HttpClient httpClient = HttpClientFactory.newHttpClient();
HttpGet httpGet = new HttpGet("some/url/to/hit");
HttpResponse httpResp = httpClient.execute(httpGet);
int statusCode = httpResp.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK)
    throw new Exception(/* ... */);

这是从特定 URL 获取 HTTP 500 响应(如在 httpResp.getStatusLine().getStatusCode() 中找到的)并抛出异常.

That's getting HTTP 500 responses (as found in the httpResp.getStatusLine().getStatusCode()) from a particular URL and throwing the exception.

问题是,当我在浏览器中访问失败"的 URL 时,它运行得非常好.

The thing is, when I go to the "failing" URL in a browser, its running perfectly fine.

所以我问:

  • HttpClient 会不会超时,缩短请求-响应周期,然后只给我一个 HTTP 500?
  • 这里还能发生什么?HttpClient 怎么可能给我 500s 当浏览器为相同的确切 URL 显示完美的页面时?
  • Could HttpClient be timing out, short-circuiting the request-response cycle, and just giving me an HTTP 500?
  • What else could be going on here? How is it possible for HttpClient to be giving me 500s when the browser is displaying the page perfectly fine for the same exact URL?

提前致谢!

推荐答案

我在访问基于 spring xml 的 api 时遇到了同样的问题.问题通过设置accept header为xml和html解决.

I had the same problem accessing a spring xml based api. The problem solved by setting the accept header to xml and html.

httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;"); 

这篇关于Apache HttpClient 被抛出 HTTP 500,但无法在浏览器中重现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆