我試圖用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