6

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(/* ... */);

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

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

So I ask:

  • 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?

Thanks in advance!

4

2 回答 2

2

我有同样的问题。我可以通过浏览器访问网站,但是在使用 apache http 客户端时,我一直收到 http 500 内部服务器错误。问题是“内容类型”GET Header。它具有“多部分/相关”的值,一些服务器似乎不喜欢。我将其更改为“text/html”,一切正常。

希望这可以帮助。

于 2012-12-15T14:06:48.813 回答
2

我在访问基于 spring xml 的 api 时遇到了同样的问题。通过将接受标头设置为 xml 和 html 解决了该问题。

httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;"); 
于 2013-11-16T17:14:46.360 回答