0

the html response i get look very bad here is my code

i try a lot of things:

Reading a web page in Java

HTTP GET request get HTML page code instead of expected response

How can I get an http response body as a string in Java?

Java HttpURLConnection Example – Java HTTP Request GET, POST

String request        = "the url i put";
    URL    url            = new URL( request );
    HttpURLConnection conn= (HttpURLConnection) url.openConnection(); 
    conn.setRequestMethod( "GET" );
    conn.setRequestProperty( "Host", "the host site"); 
    conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
    conn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Linux; Android 4.4.2; SM-G350E Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36"); 
    conn.setRequestProperty( "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    conn.setRequestProperty( "Cache-Control", "max-age=0");
    conn.setRequestProperty( "Accept-Encoding", "gzip, deflate");
    conn.setRequestProperty( "Accept-Language", "en-US,en;q=0.8");
    conn.setRequestProperty("Referer","the referer");
    conn.setRequestProperty("Cookie", "MASANG_SESSION=4e5cgroc4jp74avrjg0g07d8o0; AWSELB=0FA97BBB0EC801F2C80B193E135DA89732D0ED599FE3E2B41CAFD3B1AC5974239BBAC347246D3B929A392DD3E3ED437652A2CB236E774C97C108D1CE69E0D8E158CB02341A; sso=dcfd12917c9c8c2ee230abd3a38908d8; MASANG_SESSION=4e5cgroc4jp74avrjg0g07d8o0; _ga=GA1.2.1394751435.1517184495; _gid=GA1.2.565595090.1517184495; _gat=1");
    conn.setRequestProperty("Accept-Charset", "UTF-8"); 
    conn.connect();         
    BufferedReader in = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));      
    StringBuilder sb = new StringBuilder();
    String inputLine;  
    while ((inputLine = in.readLine()) != null) {
        sb.append(inputLine);
    }
    in.close(); 
    //print result
    System.out.println("result:::" + sb.toString());

the output response

result:::‹ىغnغFِظٍ¨نF"©‹/‰,ٹ¬$عµ,¯-'†0"G’أ)ةn`ٹ}غ—½
hema ezzat
  • 486
  • 3
  • 11

1 Answers1

0

The reason you see such "strange" response body in the output is most likely because it is gzipped. You can verify this with

System.out.println("Content-Encoding:::" + conn.getHeaderField("Content-Encoding")); 

after

conn.connect()

And you can receive gzipped response because you're sending a request with conn.setRequestProperty( "Accept-Encoding", "gzip, deflate"); (as per HTTP specificaton RFC 2616) Check how to read gzipped response for instance here http://www.rgagnon.com/javadetails/java-HttpUrlConnection-with-GZIP-encoding.html

bchto
  • 101
  • 1
  • 2