Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the whole entity from get request using httpcore library
    text
    copied!<p>I'm using <a href="http://hc.apache.org/httpcomponents-core/httpcore/index.html" rel="nofollow noreferrer">Apache Http-core library</a> 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:</p> <pre><code>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&lt;String, String&gt; 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&lt;Entry&lt;String, String&gt;&gt; hs = headers.entrySet(); Iterator&lt;Entry&lt;String, String&gt;&gt; it = hs.iterator(); Entry&lt;String, String&gt; 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&lt;String, String&gt; 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&lt;Entry&lt;String, String&gt;&gt; hs = headers.entrySet(); Iterator&lt;Entry&lt;String, String&gt;&gt; it = hs.iterator(); Entry&lt;String, String&gt; 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(); } } </code></pre> <p>When I use the method sendGetMessage, with wireshark I can see all packets, received properly, with this code:</p> <pre><code>HttpResponse response = this.HSH.sendGetMessage("http://" + this.URL + UrlSufixes.QUERY, headers); byte[] received = EntityUtils.toByteArray(response.getEntity()); </code></pre> <p>not always received has the whole entity as it was suposed to.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload