2010-03-30 3 views
0

Apache Http-core library을 사용하고 있지만 httpResponse 메시지의 엔터티에 문제가 있습니다. 가끔씩, 여러 TCP 패킷이있는 Http 응답을받을 때 마지막 패킷이 무시되는 것처럼 보입니다.httpcore 라이브러리를 사용하여 전체 엔터티를 가져 오는 방법

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

항상 수신되지가있다 : 나는 모든 패킷을 볼 수 있습니다 와이어 샤크와 방법 sendGetMessage를 사용할 때

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

는이 코드를 제대로받은 : 이 propuse에 사용되는 클래스는이 그것이 전체에 휩쓸 리게 된 전체 주체.

답변

2

응답을 완전히 읽지 않기 전에 연결을 닫으므로