我试图使用Http / 1.1 GET方法从数据记录器获取数据,但响应仅包含没有标头的http主体。我得到以下例外:
Exception in thread "main" 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:1066)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1035)
at Main.main(Main.java:42)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
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)
... 9 more
代码:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://xxx.xxxxxxx.xxx");
request.setHeader("Connection", "close");
client.execute(request);
终端:
telnet xxx.xxxxxxx.xxx 80
Trying xxx.xxxxxxx.xxx...
Connected to xxx.xxxxxxx.xxx.
Escape character is '^]'.
GET /api/1.0/getData?preserve=true HTTP/1.1
Host: xxx.xxxxxxx.xxx
Connection: close
User-Agent: Apache-HttpClient/4.2.1 (java 1.5)
{"name":"DEI2","id":"00d0694318ac","fw":"1.0.23","data":[]}
Connection closed by foreign host.
有没有办法跳过Http响应头处理并只处理响应体?
答案 0 :(得分:1)
并不是说我认为这种方法是一个好主意,但这是如何通过Apache HttpClient完成的
static class CustomHttpConnection extends DefaultBHttpClientConnection {
public CustomHttpConnection(final int buffersize) {
super(buffersize);
}
@Override
public SessionInputBuffer getSessionInputBuffer() {
return super.getSessionInputBuffer();
}
}
// Create protocol processor
HttpProcessor processor = HttpProcessorBuilder.create()
.addAll(new RequestContent(),
new RequestTargetHost(),
new RequestConnControl(),
new RequestUserAgent("HTTP/1.1"))
.build();
// Target host
InetSocketAddress target = new InetSocketAddress("localhost", 8080);
// Create custom connection that exposes its session input buffer
CustomHttpConnection conn = new CustomHttpConnection(10 * 1024);
try {
// Open socket
Socket socket = new Socket();
socket.connect(target, 5000);
// Bind it to the connection
conn.bind(socket);
// Create and initialize request
HttpGet request = new HttpGet("/");
// Force connection close
request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
// Create and initialize execution context
HttpClientContext context = HttpClientContext.create();
context.setTargetHost(new HttpHost(target.getHostName(), target.getPort()));
context.setRequestConfig(RequestConfig.DEFAULT);
context.setAttribute(HttpClientContext.HTTP_REQUEST, request);
context.setAttribute(HttpClientContext.HTTP_CONNECTION, conn);
// Process request
processor.process(request, context);
// Send request
conn.sendRequestHeader(request);
if (request instanceof HttpEntityEnclosingRequest) {
conn.sendRequestEntity(((HttpEntityEnclosingRequest) request));
}
conn.flush();
SessionInputBuffer connSessionInputBuffer = conn.getSessionInputBuffer();
IdentityInputStream instream = new IdentityInputStream(connSessionInputBuffer);
int l;
byte[] buff = new byte[1024];
while ((l = instream.read(buff)) != -1) {
System.out.println(new String(buff, 0, l, Consts.ASCII));
}
} finally {
conn.close();
}