Java 如何区分HTTPURLConnection中的请求和响应?

Java 如何区分HTTPURLConnection中的请求和响应?,java,httpurlconnection,content-encoding,Java,Httpurlconnection,Content Encoding,嗨,我需要评估服务器是否支持gzip压缩 因此,我想在请求中将gzip设置为Accept Encoding,并评估服务器的响应是否包含“Content Encoding:gzip” 但是,我不太清楚如何使用HTTPURLConnection实现这一点。特别是在查询我的连接对象的响应时。我目前: HttpURLConnection con = (HttpURLConnection) feedurl.openConnection(); con.setRequestProperty("Acce

嗨,我需要评估服务器是否支持gzip压缩

因此,我想在请求中将gzip设置为Accept Encoding,并评估服务器的响应是否包含“Content Encoding:gzip”

但是,我不太清楚如何使用HTTPURLConnection实现这一点。特别是在查询我的连接对象的响应时。我目前:

HttpURLConnection con = (HttpURLConnection) feedurl.openConnection();
    con.setRequestProperty("Accept-Encoding", "gzip");
    System.out.println("Current Accept-Encoding: "+con.getRequestProperty("Accept-Encoding"));
    //check the response for the content-size
    float feedsize = con.getContentLength()/1024f;
    //the server uses transfer-encoding=chunked and does not specify
    //a content length
    if(con.getContentLength()==-1)
    {
        //count the content size manually
                    CountingInputStream counter = new CountingInputStream(con.getInputStream());
        while(counter.read()!=-1)
        {}
        feedsize=counter.getCount()/1024f;
    }
    con.disconnect();
看一看。它说明了如何生成请求,以及如何查询响应,然后根据返回的内容创建适当的流(正常或gzip)