如何使用 httpcore 库从 get 请求中获取整个实体
我正在使用 Apache Http-core 库 但我有httpResponse 消息的实体有问题。 有时,当我收到带有多个 TCP 数据包的 Http 响应时,由于某种原因,最后一个数据包被丢弃。 用于此提案的类是这样的:
public class HttpSocketHandler
{
private String address;
private int port;
private Socket socket = null;
private HttpParams params = null;
private DefaultHttpClientConnection conn = null;
private BasicHttpProcessor httpproc;
private HttpRequestExecutor httpexecutor;
private HttpContext context;
public HttpSocketHandler(String address, int port)
{
this.params = new BasicHttpParams();
httpexecutor = new HttpRequestExecutor();
context = new BasicHttpContext(null);
this.conn = new DefaultHttpClientConnection();
this.address = address;
this.port = port;
this.httpproc = new BasicHttpProcessor();
httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());
// Recommended protocol interceptors
httpproc.addInterceptor(new RequestConnControl());
httpproc.addInterceptor(new RequestUserAgent());
httpproc.addInterceptor(new RequestExpectContinue());
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost(address,port));
}
public void InitializeConnection() throws MalformedURLException, UnknownHostException, IOException
{
if (address == null)
{
throw new MalformedURLException();
}
}
public synchronized HttpResponse sendPostMessage(String URL, Hashtable<String, String> headers, byte[] toBeSent) throws UnsupportedEncodingException, HttpException, IOException
{
this.socket = new Socket(address, port);
this.conn.bind(this.socket, this.params);
BasicHttpEntityEnclosingRequest ToSend = new BasicHttpEntityEnclosingRequest("POST", URL);
//ToSend.setEntity(new StringEntity(toBeSent, "UTF-8"));
ToSend.setEntity(new ByteArrayEntity(toBeSent));
ToSend.setParams(params);
httpexecutor.preProcess(ToSend, httpproc, context);
if (headers != null)
{
Set<Entry<String, String>> hs = headers.entrySet();
Iterator<Entry<String, String>> it = hs.iterator();
Entry<String, String> tmp;
while (it.hasNext())
{
tmp = it.next();
ToSend.setHeader(tmp.getKey(), tmp.getValue());
System.out.println(tmp.getKey() + " : " + tmp.getValue());
}
}
HttpResponse response = httpexecutor.execute(ToSend, conn, context);
response.setParams(params);
this.conn.close();
return response;
}
public synchronized HttpResponse sendGetMessage(String URL, Hashtable<String, String> headers) throws IOException, HttpException
{
this.socket = new Socket(address, port);
this.conn.bind(this.socket, this.params);
BasicHttpRequest ToSend = new BasicHttpRequest("GET", URL);
ToSend.setParams(this.params);
httpexecutor.preProcess(ToSend, httpproc, context);
if (headers != null)
{
Set<Entry<String, String>> hs = headers.entrySet();
Iterator<Entry<String, String>> it = hs.iterator();
Entry<String, String> tmp;
while (it.hasNext())
{
tmp = it.next();
ToSend.setHeader(tmp.getKey(), tmp.getValue());
}
}
HttpResponse response = httpexecutor.execute(ToSend, conn, context);
response.setParams(params);
this.conn.close();
this.httpexecutor.postProcess(response, this.httpproc, this.context);
//System.out.println(response.getEntity().getContentLength());
//return EntityUtils.toString(response.getEntity());
return response;
}
public void close() throws IOException
{
this.conn.close();
}
}
当我使用wireshark的sendGetMessage方法时,我可以看到正确接收的所有数据包,使用以下代码:
HttpResponse response = this.HSH.sendGetMessage("http://" + this.URL + UrlSufixes.QUERY, headers);
byte[] received = EntityUtils.toByteArray(response.getEntity());
并不总是收到预期的整个实体。
I'm using Apache Http-core library but i'm having a problem with the entity of the httpResponse message.
It seems like sometimes, when I receive a Http response, with multiple TCP packets, the last packet is discarded, for some reason.
The class used for this propuse is this:
public class HttpSocketHandler
{
private String address;
private int port;
private Socket socket = null;
private HttpParams params = null;
private DefaultHttpClientConnection conn = null;
private BasicHttpProcessor httpproc;
private HttpRequestExecutor httpexecutor;
private HttpContext context;
public HttpSocketHandler(String address, int port)
{
this.params = new BasicHttpParams();
httpexecutor = new HttpRequestExecutor();
context = new BasicHttpContext(null);
this.conn = new DefaultHttpClientConnection();
this.address = address;
this.port = port;
this.httpproc = new BasicHttpProcessor();
httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());
// Recommended protocol interceptors
httpproc.addInterceptor(new RequestConnControl());
httpproc.addInterceptor(new RequestUserAgent());
httpproc.addInterceptor(new RequestExpectContinue());
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost(address,port));
}
public void InitializeConnection() throws MalformedURLException, UnknownHostException, IOException
{
if (address == null)
{
throw new MalformedURLException();
}
}
public synchronized HttpResponse sendPostMessage(String URL, Hashtable<String, String> headers, byte[] toBeSent) throws UnsupportedEncodingException, HttpException, IOException
{
this.socket = new Socket(address, port);
this.conn.bind(this.socket, this.params);
BasicHttpEntityEnclosingRequest ToSend = new BasicHttpEntityEnclosingRequest("POST", URL);
//ToSend.setEntity(new StringEntity(toBeSent, "UTF-8"));
ToSend.setEntity(new ByteArrayEntity(toBeSent));
ToSend.setParams(params);
httpexecutor.preProcess(ToSend, httpproc, context);
if (headers != null)
{
Set<Entry<String, String>> hs = headers.entrySet();
Iterator<Entry<String, String>> it = hs.iterator();
Entry<String, String> tmp;
while (it.hasNext())
{
tmp = it.next();
ToSend.setHeader(tmp.getKey(), tmp.getValue());
System.out.println(tmp.getKey() + " : " + tmp.getValue());
}
}
HttpResponse response = httpexecutor.execute(ToSend, conn, context);
response.setParams(params);
this.conn.close();
return response;
}
public synchronized HttpResponse sendGetMessage(String URL, Hashtable<String, String> headers) throws IOException, HttpException
{
this.socket = new Socket(address, port);
this.conn.bind(this.socket, this.params);
BasicHttpRequest ToSend = new BasicHttpRequest("GET", URL);
ToSend.setParams(this.params);
httpexecutor.preProcess(ToSend, httpproc, context);
if (headers != null)
{
Set<Entry<String, String>> hs = headers.entrySet();
Iterator<Entry<String, String>> it = hs.iterator();
Entry<String, String> tmp;
while (it.hasNext())
{
tmp = it.next();
ToSend.setHeader(tmp.getKey(), tmp.getValue());
}
}
HttpResponse response = httpexecutor.execute(ToSend, conn, context);
response.setParams(params);
this.conn.close();
this.httpexecutor.postProcess(response, this.httpproc, this.context);
//System.out.println(response.getEntity().getContentLength());
//return EntityUtils.toString(response.getEntity());
return response;
}
public void close() throws IOException
{
this.conn.close();
}
}
When I use the method sendGetMessage, with wireshark I can see all packets, received properly, with this code:
HttpResponse response = this.HSH.sendGetMessage("http://" + this.URL + UrlSufixes.QUERY, headers);
byte[] received = EntityUtils.toByteArray(response.getEntity());
not always received has the whole entity as it was suposed to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您在完全读取响应之前关闭了连接
Because you close connection before the response is fully read