每隔一个使用 Apache HTTPClient 的请求就会失败 [英] Every second request using Apache HTTPClient fails

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

问题描述

我正在尝试使用 Apache HTTPClient 4.5.1 来做一些休息请求.不幸的是,每第二个请求都以java.net.SocketTimeoutException: Read timed out"结束(如果未设置套接字超时,则永远挂起).

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天全站免登陆