我试图清除squid上的缓存,为了做到这一点,我需要执行奇怪的http请求。
请求应如下所示:
PURGE www.cached:port/params HTTP/1.1
其中www.cached:port/params
表示我想从缓存中删除的值。
所以,这是有趣的事情 - 连接应该打开一个squid服务器,而不是www.cached:port/params
。
所以,整个序列将是:
PURGE
请求。我试过apache httpclient。我可以重写请求方法以使其发送PURGE
,但库总是打开与http请求中传递的同一主机的连接(打开与www.cached
的连接,执行PURGE
{{1} })它对我不起作用。
我可以通过使用纯套接字来实现,但找到一个可以正常工作的库会很棒。
答案 0 :(得分:0)
首先,我认为没有理由不能用Apache HttpClient来完成。您是否将客户端配置为使用'http.route.default-proxy'参数通过代理执行请求?
无论如何,如果你不介意使用较低的组件,这就是用Apache HttpCore(阻塞)完成同样的工作
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();
}
}
我确信其他HTTP库非常可行