Come faccio a differenziare tra la richiesta e la risposta in un HttpURLConnection?
-
16-09-2019 - |
Domanda
Ciao ho bisogno di valutare se un server supporta la compressione gzip.
Per questo voglio impostare gzip come Accept-Encoding nella mia richiesta e valutare se la risposta del server di containts "Content-Encoding: gzip".
Comunque io non sono del tutto chiaro su come fare questo con HttpURLConnection. Soprattutto quando per interrogare il mio oggetto Connection circa la risposta. Io attualmente faccio:
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();
Soluzione
Dai un'occhiata alla questo articolo OReilly . Esso illustra come generare la richiesta, e come per interrogare la risposta e quindi creare il flusso appropriato (normale o gzip) dipende da ciò che è stato restituito.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow