如何请求和响应在HttpURLConnection的区分?
-
16-09-2019 - |
题
您好我需要如果服务器支持gzip压缩来评价。
所以我想设置的gzip作为的Accept-Encoding在我的要求和评估,如果服务器containts的响应“内容编码: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();
解决方案
看一看这奥赖利文章。它说明如何生成请求,以及如何询问响应,然后创建适当的流(正常或gzip压缩)依赖于被返回了什么。
不隶属于 StackOverflow