2010-03-30 66 views
0

我正在使用Apache Http-core library,但我遇到了httpResponse消息實體的問題。 看起來有時候,當我收到一個Http響應,有多個TCP數據包時,由於某種原因,最後一個數據包被丟棄。 用於此propuse類是這樣的:如何使用httpcore庫獲取整個實體獲取請求

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(); 
} 
} 

當我使用的方法sendGetMessage,使用Wireshark我可以看到所有的數據包,正常接收到的,與此代碼:

HttpResponse response = this.HSH.sendGetMessage("http://" + this.URL + UrlSufixes.QUERY, headers); 
byte[] received = EntityUtils.toByteArray(response.getEntity()); 

並不總是收到有就像它所向往的整個實體一樣。

回答

2

因爲在響應完全讀取之前關閉連接