使用Apache HTTPClient的第二个请求失败 [英] Every second request using Apache HTTPClient fails

查看:181
本文介绍了使用Apache HTTPClient的第二个请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache HTTPClient 4.5.1进行一些休息请求.不幸的是,第二个请求最终都以"java.net.SocketTimeoutException:读取超时"结束(或者,如果未设置套接字超时,则永久挂起).

I am trying to use Apache HTTPClient 4.5.1 to do some rest requests. Unfortunately every second request ends up in "java.net.SocketTimeoutException: Read timed out" (or hangs forever if the socket timeout is not set).

我正在这样建立我的客户:

I am building my client like this:

ConnectionSocketFactory sf = new PlainConnectionSocketFactory();

Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory> create()
    .register("http", sf)
    .build();

Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider> create()
    .register(AuthSchemes.BASIC, (AuthSchemeProvider) new BasicSchemeFactory())
    .build();

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r);

CredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass"));

RequestConfig requestConfig = RequestConfig.custom()
    .setConnectTimeout(15000)
    .setConnectionRequestTimeout(15000)
    .setSocketTimeout(15000)
    .build();

this.client = HttpClients.custom()
    .setConnectionManager(cm)
    .setDefaultCredentialsProvider(cp)
    .setDefaultAuthSchemeRegistry(authProviders)
    .setDefaultRequestConfig(requestConfig)
    .build();

然后,我在相同的HttpClient实例上执行这样的请求:

Afterwards I do my requests like this (on the same HttpClient instance):

HttpDelete delete = new HttpDelete(uri);

HttpClientContext context = HttpClientContext.create();
CloseableHttpResponse response = this.client.execute(request, context);
try {
    int statusCode = response.getStatusLine().getStatusCode();
    return statusCode;
}
finally {
    response.close();
}

如果我为每个请求开始使用新的HttpClient实例,那么一切都会很好. 在服务器端,我正在运行wildfly 8(还有9).对于第二个请求,我什至都看不到请求的传入,因此对我来说,客户端似乎根本没有尝试.

Everything works fine if I start using a new HttpClient instance for every request. On the server side I have a wildfly 8 (and also 9) running. For the second request I cannot even see a request incoming, so to me it looks like the client is not even trying.

对我遗失/做错的事情有什么想法吗?

Any ideas on what I am missing/doing wrong?

推荐答案

感谢 hotzst 的提示,以便启用日志记录,我知道了:

Thanks to the hint from hotzst to enable logging, I figured it out:

服务器正在重设HTTP状态204(无内容),但是无论如何都发送了一些响应数据. HttpClient将这些响应数据作为NEXT请求的响应中的垃圾",通过拧紧响应头将其破坏.将储备者响应代码更改为404可以解决我的问题.

The server was returing a HTTP status 204 (No content), but sent some response data anyway. HttpClient got those response data as "Garbage in response" for the NEXT request, which broke it by screwing up the response headers. Changing the reservers response code to 404 fixes the problem for me.

这篇关于使用Apache HTTPClient的第二个请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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