target:http://www.52rd.com/bbs/Detail_RD.BBS_185755_11_1_1.html
charset:gb2312症状,获取html源码不完整,会是JDK的bug吗?用Apache的httpClient试过,一切正常,就URLConnection不行。贴上方法:public static String html2String(String sUrl,String charset) throws IOException { 
    int HttpResult; 
    URL url = new URL(sUrl); 
    URLConnection urlconn = url.openConnection();
    StringBuffer resultBuffer = new StringBuffer();
    urlconn.connect(); 
    HttpURLConnection httpconn = (HttpURLConnection) urlconn;
    HttpResult = httpconn.getResponseCode(); 
    if (HttpResult == HttpURLConnection.HTTP_OK) { 
      InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(),charset); 
      BufferedReader in = new BufferedReader(isr); 
      String inputLine = null; 
      while ((inputLine = in.readLine()) != null) { 
       resultBuffer.append(inputLine+"\n");
      }
      in.close(); 
    } 
    return resultBuffer.toString();

解决方案 »

  1.   

    Getting Text from a URL
        try {
            // Create a URL for the desired page
            URL url = new URL("http://hostname:80/index.html");
        
            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                // str is one line of text; readLine() strips the newline character(s)
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
      

  2.   

    同问
    返回头没有content-length,结果就读不完