HttpURLConnection 从网页获取内容与乱码问题解决

//此方法为从指定URL获取网页内容


public static String getHtmlConentByUrl(

   String ssourl) {

   //ssourl为指定的URL

   try {    
    URL url = new URL(ssourl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    //此处为处理中文乱码的解决方法
    con.setRequestProperty("contentType", "GBK");
    con.setInstanceFollowRedirects(false);
    con.setUseCaches(false);
    con.setAllowUserInteraction(false);
    con.connect();


    StringBuffer sb = new StringBuffer();

    String line = "";

//此处为处理中文乱码的解决方法

    BufferedReader URLinput = new BufferedReader(new InputStreamReader(con.getInputStream(),"GBK"));
    while ((line = URLinput.readLine()) != null) {
     sb.append(line);
    } 
    con.disconnect(); 
  
    return sb.toString().toLowerCase();
   } catch (Exception e) {
     
    return null;
   }
}

你可能感兴趣的:(HttpURLConnection 从网页获取内容与乱码问题解决)