Ich versuche, ein PURGE mit HttpURLConnection wie folgt auszuführen:Wie mache ich HTTP-Bereinigung von Java?
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();
}
}
}
Aber ich bin immer:
08:56:31,813 ERROR [VarnishHandler] Could not call varnish: java.net.ProtocolException: Invalid HTTP method: PURGE
Mit curl gibt es kein Problem:
curl -I - X PURGE -H "Host: 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
Also wie mache ich th ist? Muss ich mich von Java locken lassen oder gibt es eine andere Bibliothek, die ich benutzen kann?
Vielen Dank! Ich endete mit der Erweiterung von HttpMethodBase in apache.commons.httpclient – jakob
HttpMethodBase ist Teil des Commons HttpClient-Projekts, das eigentlich als "Ende des Lebens" markiert ist. Sehen Sie mehr hier http://hc.apache.org/httpclient-3.x/. Wenn Sie auf dem neuesten Stand sein wollen, sollten Sie HttpComponents HttpClient verwenden: http://hc.apache.org/httpcomponents-client-ga/index.html – uldall
Ok ich verstehe! Ich habe stattdessen httpcomponents implementiert. – jakob