どのように私はHttpURLConnectionの中で、要求と応答を区別するのですか?
-
16-09-2019 - |
質問
こんにちは、私は、サーバーがGZIP圧縮をサポートしているかどうかを評価する必要があります。
「:gzipのコンテンツエンコーディング」そこで私は、私のリクエストで、エンコーディングが受け入れた場合、サーバー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();