java - HttpURLConnection response is incorrect -


when using code below make request:

private string get(string inurl, map headers, boolean followredirects) throws malformedurlexception, ioexception {          url url = new url(inurl);         httpurlconnection connection = (httpurlconnection) url.openconnection();         connection.setinstancefollowredirects(followredirects);           // add headers request.         iterator entries = headers.entryset().iterator();         while (entries.hasnext()) {             entry thisentry = (entry) entries.next();             object key = thisentry.getkey();             object value = thisentry.getvalue();             connection.addrequestproperty((string)key, (string)value);         }          // attempt parse         inputstream stream = connection.getinputstream();         inputstreamreader isreader = new inputstreamreader(stream );          bufferedreader br = new bufferedreader(isreader );         system.out.println(br.readline());          // disconnect         connection.disconnect();          return connection.getheaderfield("location"); } 

the resulting response nonsensical (e.g ���:ks�6��﯐9�rђ� e��u�n�qש�v���"ui*�w��s)

however can see in wireshark response html/xml , nothing string above. i've tried myriad of different methods parsing inputstream same result each time.

please note: happens when it's html/xml, plain html works.

why response coming in format?

thanks in advance!

=== solved ===

gah, got it!

the server compressing response when contains xml, needed use gzipinputstream instead of inputsream.

gzipinputstream stream = new gzipinputstream(connection.getinputstream()); 

thanks anyway!

use utf-8 encoding in input stream below

inputstreamreader isreader = new inputstreamreader(stream, "utf-8");  

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -