您的位置首页生活百科

为什么用JAVA获取不了这个网页的源代码

为什么用JAVA获取不了这个网页的源代码

好诡异.....................试了很多次才弄好!!!!!!!!!!!!!import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class test1 { public static void main(String[] a) throws IOException { String url = "http://video.shishicai.cn/haoma/gd11x5/list/2010-07-16.aspx";System.out.println(getHTML(url, "utf-8")); //使用原念信网页里声明的gb2312反而会慧高渣出现乱码前悄 } public static String getHTML(String pageURL, String encoding) { StringBuilder pageHTML = new StringBuilder(); try { URL url = new URL(pageURL); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestProperty("User-Agent", "MSIE 7.0"); BufferedReader br = new BufferedReader(new InputStreamReader( connection.getInputStream(), encoding)); String line = null; while ((line = br.readLine()) != null) { pageHTML.append(line); pageHTML.append("\r\n"); } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return pageHTML.toString(); }}