Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>By using an <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStreamReader.html#InputStreamReader%28java.io.InputStream,%20java.nio.charset.Charset%29" rel="nofollow">InputStreamReader</a> and specifying your charset, like so:</p> <pre><code>inStr = new InputStreamReader(InputStream, Charset.forName("GB2312")); </code></pre> <p>The following code worked for me:</p> <pre><code>import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.Charset; import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; public class Foo { public static void main(String[] args) { System.out.println(downloadHtml("http://baike.baidu.com/view/6000001.htm")); } private static String downloadHtml(String urlString) { URL url = null; InputStream inStr = null; StringBuffer buffer = new StringBuffer(); try { url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail HttpURLConnection.setFollowRedirects(true); // allow both GZip and Deflate (ZLib) encodings //conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); String encoding = conn.getContentEncoding(); inStr = null; // create the appropriate stream wrapper based on // the encoding type if (encoding != null &amp;&amp; encoding.equalsIgnoreCase("gzip")) { inStr = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null &amp;&amp; encoding.equalsIgnoreCase("deflate")) { inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); } else { inStr = conn.getInputStream(); } int ptr = 0; InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312")); while ((ptr = inStrReader.read()) != -1) { buffer.append((char)ptr); } inStrReader.close(); conn.disconnect(); } catch(Exception e) { e.printStackTrace(); } finally { if (inStr != null) try { inStr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return buffer.toString(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload