我试图用HttpURLConnection的这样进行吹扫:如何从Java进行HTTP清除?
private void callVarnish(URL url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(PURGE_METHOD);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("Host", "www.somehost.com");
conn.connect();
System.out.print(conn.getResponseCode() + " " + conn.getResponseMessage());
}
catch (Exception e) {
log.error("Could not call varnish: " + e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
但我发现了:
08:56:31,813 ERROR [VarnishHandler] Could not call varnish: java.net.ProtocolException: Invalid HTTP method: PURGE
,卷曲没有问题:
卷曲-I - X PURGE -H“主机:www.somehost.com”someurl
HTTP/1.1 404 Not in cache.
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 401
Accept-Ranges: bytes
Date: Thu, 18 Oct 2012 06:40:19 GMT
X-Varnish: 1611365598
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS
那么我该怎么做是什么?我是否需要从Java中进行卷曲或者是否有其他一些可以使用的库?
谢谢!我结束了扩展HttpMethodBase在apache.commons.httpclient – jakob
HttpMethodBase是Commons HttpClient项目的一部分,它实际上标记为“生命的尽头”。在这里看到更多http://hc.apache.org/httpclient-3.x/。如果你想要更新你应该使用HttpComponents HttpClient:http://hc.apache.org/httpcomponents-client-ga/index.html – uldall
好吧,我明白!我已经实现了httpcomponents。 – jakob