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

查看:824
本文介绍了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?
  • 这里还会发生什么?当浏览器显示的页面完全适合相同的URL时,HttpClient如何给我500分?
  • 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标头设置为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天全站免登陆