您好我需要评估服务器是否支持gzip压缩。
因此,我想在我的请求中将gzip设置为Accept-Encoding,并评估服务器的响应是否包含“Content-Encoding:gzip”。
但是我不太清楚如何使用HTTPURLConnection执行此操作。特别是在查询我的Connection对象时的响应。我现在这样做:
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();