httpurlconnection에서 요청과 응답을 어떻게 구별합니까?
-
16-09-2019 - |
문제
안녕하세요 서버가 GZIP 압축을 지원하는지 평가해야합니다.
따라서 요청에서 GZIP를 수락 인코딩으로 설정하고 서버의 응답이 "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();
해결책
살펴보십시오 이 오레일리 기사. 요청을 생성하는 방법과 응답을 조사한 다음 반환되는 내용에 따라 적절한 스트림 (정상 또는 GZIPPER)을 작성하는 방법을 보여줍니다.
제휴하지 않습니다 StackOverflow