2012-11-15 5 views
1

squid에 캐시를 비우려고하고 있는데 이상한 http 요청을 실행해야합니다. www.cached:port/params이 값을 나타냅니다Java 용 유연한 저수준 http 라이브러리?

PURGE www.cached:port/params HTTP/1.1 

내가 캐시에서 제거 할 : 같은

요청이 보일 것입니다.

재미있는 점이 있습니다 - 연결은 www.cached:port/params이 아니라 오징어 서버로 열어야합니다.

그래서, 전체 순서는 다음과 같습니다 오징어 서버에

  1. 열린 연결.
  2. 패스 PURGE 요청.
  3. 읽기 결과. 가까운

  • 나는 아파치 HttpClient를 시도했다. 요청 방법을 다시 작성하여 PURGE을 보내도록 할 수는 있지만 라이브러리는 http 요청 (예 : www.cached에 대한 연결을 열고 PURGEwww.cached으로 연결)과 동일한 호스트에 대한 연결을 항상 열어 주며 그냥 작동하지 않습니다.

    순수 소켓을 사용하여 수행 할 수 있지만 작동하는 라이브러리를 찾는 것이 좋습니다.

  • +0

    https://netty.io/를 보셨습니까? – ollins

    +0

    작동 할 수 있습니다. DefaultHttpRequest에서 toString을 재정의 할 수 있습니다. 내일 할거야. – Alex

    답변

    0

    먼저, Apache HttpClient를 사용하여이 작업을 수행 할 수없는 이유는 없습니다. 'http.route.default-proxy'매개 변수를 사용하여 프록시를 통해 요청을 실행하도록 클라이언트를 구성 했습니까? 당신이이 같은 아파치 HttpCore 함께 할 수있는 방법이다 낮은 구성 요소를 사용하여 괜찮다면

    어쨌든, 같은 다른 HTTP 라이브러리 매우 행할 확신

    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { 
          new RequestContent(), 
          new RequestTargetHost(), 
          new RequestConnControl(), 
          new RequestUserAgent()}); 
    
        HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); 
        ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); 
    
        HttpContext context = new BasicHttpContext(); 
        HttpHost target = new HttpHost("www.cached", port); 
        HttpHost proxy = new HttpHost("squid", 8080); 
    
        HttpParams params = new BasicHttpParams(); 
        HttpRequest request = new BasicHttpRequest("PURGE", "www.cached:port/params"); 
    
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); 
        try { 
         if (!conn.isOpen()) { 
          Socket socket = new Socket(proxy.getHostName(), proxy.getPort()); 
          conn.bind(socket, params); 
         } 
         context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); 
         context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); 
    
         httpexecutor.preProcess(request, httpproc, context); 
         HttpResponse response = httpexecutor.execute(request, conn, context); 
         httpexecutor.postProcess(response, httpproc, context); 
    
         // do something useful with the response 
    
         if (!connStrategy.keepAlive(response, context)) { 
          conn.close(); 
         } else { 
          // Connection could be kept alive 
         } 
        } finally { 
         conn.close(); 
        } 
    } 
    

    (차단)