Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Java中清除HTTP?_Java_Httpurlconnection_Varnish_Http Method - Fatal编程技术网

如何从Java中清除HTTP?

如何从Java中清除HTTP?,java,httpurlconnection,varnish,http-method,Java,Httpurlconnection,Varnish,Http Method,我正在尝试使用HttpUrlConnection执行如下清除: private void callVarnish(URL url) { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(PURGE_METHOD); conn.setDoOutput(true);

我正在尝试使用HttpUrlConnection执行如下清除:

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


那我该怎么做呢?我需要从Java中卷曲还是有其他库可以使用?

您可以使用Apache的HttpClient库:

您可以使用或实现自己的HttpPurge类扩展

您可以在此处找到快速入门指南:

例如:

DefaultHttpClient httpclient = new DefaultHttpClient();
BasicHttpRequest httpPurge = new BasicHttpRequest("PURGE", "www.somehost.com") 
HttpResponse response = httpclient.execute(httpPurge);

使用org.apache.httpcomponents 4.2.1:

类别:

import org.apache.http.client.methods.HttpRequestBase;

import java.net.URI;

public class HttpPurge extends HttpRequestBase {

    public final static String METHOD_NAME = "PURGE";

    public HttpPurge() {
        super();
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public HttpPurge(final String uri) {
        super();
        setURI(URI.create(uri));
    }

    public String getName() {
        return "PURGE";
    }
}
import org.apache.commons.httpclient.HttpMethodBase;

public class PurgeMethod extends HttpMethodBase {

    public PurgeMethod() {
        super();
        setFollowRedirects(true);
    }

    public PurgeMethod(String url) {
        super(url);
        setFollowRedirects(true);
    }

    public String getName() {
        return "PURGE";
    }
}
电话:

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import test.HttpPurge

private void callVarnish(URL url) {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPurge httpPurge = new HttpPurge(url.toString());
        Header header = new BasicHeader("Host", "www.somewhere.se");
        httpPurge.setHeader(header);
        try {
            HttpResponse response = httpclient.execute(httpPurge);
            System.out.print("-------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.print("-------------------------------------");
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release
            if (entity != null) {
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity);
            }
        } catch (IOException ex) {

            // In case of an IOException the connection will be released
            // back to the connection manager automatically
        } catch (RuntimeException ex) {

            // In case of an unexpected exception you may want to abort
            // the HTTP request in order to shut down the underlying
            // connection and release it back to the connection manager.
            httpPurge.abort();
        }
}
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;

private void callVarnish(URL url) {
    HttpClient client = new HttpClient();
    HttpMethod method = new PurgeMethod(url.toString());

try {
    int status = 0;
    status = client.executeMethod(method);

    log.debug(status);
} catch (Exception e) {
    // something
} finally {
    method.releaseConnection();
}
对于不推荐使用的org.apache.commons.httpclient.HttpMethodBase:

类别:

import org.apache.http.client.methods.HttpRequestBase;

import java.net.URI;

public class HttpPurge extends HttpRequestBase {

    public final static String METHOD_NAME = "PURGE";

    public HttpPurge() {
        super();
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public HttpPurge(final String uri) {
        super();
        setURI(URI.create(uri));
    }

    public String getName() {
        return "PURGE";
    }
}
import org.apache.commons.httpclient.HttpMethodBase;

public class PurgeMethod extends HttpMethodBase {

    public PurgeMethod() {
        super();
        setFollowRedirects(true);
    }

    public PurgeMethod(String url) {
        super(url);
        setFollowRedirects(true);
    }

    public String getName() {
        return "PURGE";
    }
}
电话:

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import test.HttpPurge

private void callVarnish(URL url) {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPurge httpPurge = new HttpPurge(url.toString());
        Header header = new BasicHeader("Host", "www.somewhere.se");
        httpPurge.setHeader(header);
        try {
            HttpResponse response = httpclient.execute(httpPurge);
            System.out.print("-------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.print("-------------------------------------");
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release
            if (entity != null) {
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity);
            }
        } catch (IOException ex) {

            // In case of an IOException the connection will be released
            // back to the connection manager automatically
        } catch (RuntimeException ex) {

            // In case of an unexpected exception you may want to abort
            // the HTTP request in order to shut down the underlying
            // connection and release it back to the connection manager.
            httpPurge.abort();
        }
}
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;

private void callVarnish(URL url) {
    HttpClient client = new HttpClient();
    HttpMethod method = new PurgeMethod(url.toString());

try {
    int status = 0;
    status = client.executeMethod(method);

    log.debug(status);
} catch (Exception e) {
    // something
} finally {
    method.releaseConnection();
}

}

使用HttpComponents时,API发生了变化。当前版本的uldall答案如下:

HttpHost host = new HttpHost(hostname, port);
HttpClient httpclient = HttpClientBuilder.create().build();
BasicHttpRequest purgeRequest = new BasicHttpRequest("PURGE", "/some/url");
HttpResponse response = httpclient.execute(host, purgeRequest);

非常感谢。我最终在apache.commons.httpclientHttpMethodBase中扩展了HttpMethodBase。httpclientHttpMethodBase是commons HttpClient项目的一部分,该项目实际上被标记为“生命的终结”。在这里看到更多。如果你想成为最新的,你应该使用HttpComponents HttpClient:好的,我明白了!我已经实现了httpcomponents。有没有办法扩展本机HttpUrlConnection等来支持清除请求?我们在Java中为自定义“HTTP客户机”包装本机HTTP支持,而不是使用第三方库(如Apache)。保持这种状态比不得不靠岸要好,尽管我们可以这样做作为最后的手段。