Apache HttpClient is throwng HTTP 500s but can't reproduce in a browser

不打扰是莪最后的温柔 提交于 2020-01-03 12:44:43

问题


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!


回答1:


I had the same problem. I could access a web site via the browser but when using the apache http client, I consistently got http 500 internal server error. The problem was the "Content-Type" GET Header. It had the value "multipart/related",which some servers don't seem to like. I changed it to "text/html" and it all worked fine.

Hope this helps.




回答2:


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;"); 


来源:https://stackoverflow.com/questions/12954331/apache-httpclient-is-throwng-http-500s-but-cant-reproduce-in-a-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!