我正在使用apache HttpClient。在调试某些东西时,我发现当我第二次使用相同的连接(连接保持活动)执行相同的HTTPS HEAD请求时出现错误。主持人是https://dongshengcn.iriscouch.com。我不确定哪里出错了,是服务器吗?它是一个HttpClient错误?我倾向于认为这是一个HttpClient错误。无论如何,我想知道是否有人可以指出我正确的方向。这是代码和日志信息:
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.junit.Test;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* User: dongsheng
* Date: 1/10/13
* Time: 4:10 PM
* To change this template use File | Settings | File Templates.
*/
public class HttpClientTest {
private int connectionTimeout = 1000;
private int socketTimeout = 10000;
private String host = "dongshengcn.iriscouch.com";
// private String host = "dongshengcn.cloudant.com";
private int port = 443;
boolean useExpectContinue = true;
public DefaultHttpClient configureClient() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setUseExpectContinue(params, useExpectContinue);
HttpConnectionParams
.setConnectionTimeout(params, connectionTimeout);
HttpConnectionParams.setSoTimeout(params, socketTimeout);
HttpConnectionParams.setTcpNoDelay(params, Boolean.TRUE);
String protocol = "https";
params.setParameter(ClientPNames.DEFAULT_HOST, new HttpHost(host,
port, protocol));
DefaultHttpClient client = new DefaultHttpClient(params);
return client;
}
@Test
public void test_dafault_http_client() throws IOException {
String uri = "/ektorp-test/";
DefaultHttpClient client = configureClient();
org.apache.http.HttpResponse rsp = client.execute(new HttpHead(uri));
System.out.println(rsp.toString());
try {
org.apache.http.HttpResponse rsp2 = client.execute(new HttpHead(uri));
System.out.println(rsp.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Running HttpClientTest
2013/01/11 10:12:05:288 EST [DEBUG] BasicClientConnectionManager - Get connection for route {s}->https://dongshengcn.iriscouch.com:443
2013/01/11 10:12:06:065 EST [DEBUG] DefaultClientConnectionOperator - Connecting to dongshengcn.iriscouch.com:443
2013/01/11 10:12:07:201 EST [DEBUG] RequestAddCookies - CookieSpec selected: best-match
2013/01/11 10:12:07:223 EST [DEBUG] RequestAuthCache - Auth cache not set in the context
2013/01/11 10:12:07:223 EST [DEBUG] RequestTargetAuthentication - Target auth state: UNCHALLENGED
2013/01/11 10:12:07:224 EST [DEBUG] RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
2013/01/11 10:12:07:224 EST [DEBUG] DefaultHttpClient - Attempt 1 to execute request
2013/01/11 10:12:07:224 EST [DEBUG] DefaultClientConnection - Sending request: HEAD /ektorp-test/ HTTP/1.1
2013/01/11 10:12:07:226 EST [DEBUG] headers - >> HEAD /ektorp-test/ HTTP/1.1
2013/01/11 10:12:07:226 EST [DEBUG] headers - >> Host: dongshengcn.iriscouch.com:443
2013/01/11 10:12:07:226 EST [DEBUG] headers - >> Connection: Keep-Alive
2013/01/11 10:12:07:303 EST [DEBUG] DefaultClientConnection - Receiving response: HTTP/1.1 404 Host not found
2013/01/11 10:12:07:304 EST [DEBUG] headers - << HTTP/1.1 404 Host not found
2013/01/11 10:12:07:304 EST [DEBUG] headers - << Content-Length: 14
2013/01/11 10:12:07:307 EST [DEBUG] DefaultHttpClient - Connection can be kept alive indefinitely
2013/01/11 10:12:07:309 EST [DEBUG] BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@21cc5069
2013/01/11 10:12:07:309 EST [DEBUG] BasicClientConnectionManager - Connection can be kept alive indefinitely
HTTP/1.1 404 Host not found [Content-Length: 14]
2013/01/11 10:12:07:309 EST [DEBUG] BasicClientConnectionManager - Get connection for route {s}->https://dongshengcn.iriscouch.com:443
2013/01/11 10:12:07:310 EST [DEBUG] DefaultHttpClient - Stale connection check
2013/01/11 10:12:07:311 EST [DEBUG] RequestAddCookies - CookieSpec selected: best-match
2013/01/11 10:12:07:311 EST [DEBUG] RequestAuthCache - Auth cache not set in the context
2013/01/11 10:12:07:311 EST [DEBUG] RequestTargetAuthentication - Target auth state: UNCHALLENGED
2013/01/11 10:12:07:311 EST [DEBUG] RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
2013/01/11 10:12:07:311 EST [DEBUG] DefaultHttpClient - Attempt 1 to execute request
2013/01/11 10:12:07:311 EST [DEBUG] DefaultClientConnection - Sending request: HEAD /ektorp-test/ HTTP/1.1
2013/01/11 10:12:07:311 EST [DEBUG] headers - >> HEAD /ektorp-test/ HTTP/1.1
2013/01/11 10:12:07:311 EST [DEBUG] headers - >> Host: dongshengcn.iriscouch.com:443
2013/01/11 10:12:07:311 EST [DEBUG] headers - >> Connection: Keep-Alive
2013/01/11 10:12:07:314 EST [DEBUG] DefaultHttpResponseParser - Garbage in response: Host not found
2013/01/11 10:12:07:314 EST [DEBUG] DefaultClientConnection - Connection 0.0.0.0:62168<->173.192.57.98:443 closed
2013/01/11 10:12:07:315 EST [DEBUG] DefaultClientConnection - Connection 0.0.0.0:62168<->173.192.57.98:443 shut down
2013/01/11 10:12:07:315 EST [DEBUG] BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@67c7980c
2013/01/11 10:12:07:315 EST [DEBUG] BasicClientConnectionManager - Connection can be kept alive for 9223372036854775807 MILLISECONDS
org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at HttpClientTest.test_dafault_http_client(HttpClientTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:103)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
... 32 more
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.411 sec
如果我将主机更改为dongshengcn.cloudant.com:443之类的主机,它会按预期工作。
答案 0 :(得分:1)
服务器很可能无法正确处理HEAD请求并发送响应主体以响应第一个请求,违反了HTTP规范,破坏了连接状态并使其不可重用。