【发布时间】:2012-10-08 14:30:39
【问题描述】:
我正在尝试使用 HttpUrlConnection 执行 PURGE,如下所示:
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
使用 curl 没有问题:
curl -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 卷曲还是有其他可以使用的库?
【问题讨论】:
标签: java httpurlconnection varnish http-method