1
回答
java URLConnection 获取页面内容的问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
public class HaGongDa {
    public static void main(String[] args)  {
        //String s1 = sendGet("http://today.hit.edu.cn/news/2015/07-08/9461048070RL0.htm","");
        //System.out.println(s1);
        try {
            Document doc = Jsoup.connect("http://today.hit.edu.cn/news/2015/07-08/9461048070RL0.htm").get();
            String html = doc.html();
            System.out.println(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String sendGet (String url,String param) {
        String result="";
        BufferedReader in = null;
        try {
            String urlNameString;
            if(!param.equals("")) {
                urlNameString = url + "?" + param;
            }else {
                urlNameString = url;
            }

            URL realUrl = new URL(urlNameString);
            //打开连接
            URLConnection connection = realUrl.openConnection();
            //设置通用的请求属性
            connection.setRequestProperty("accept","*/*");
            connection.setRequestProperty("connection","Keep-Alive");
            connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            //建立实际的连接
            connection.connect();
            //获取所有响应头字段
            Map map = connection.getHeaderFields();
            //遍历响应头字段
            for (String key : map.keySet()){
                System.out.println(key + "--->" + map.get(key));
            }
            //定义 bufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine())!=null) {
                result += line;
            }

        }catch (Exception e) {
            System.out.println("发送Get请求出现异常!" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if(in != null) {
                    in.close();
                }
            }catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
}

希望获取到iframe标签下的#document 内容,尝试使用URLConnection 和 jsoup 都不行

而实际获取到的

求教
举报
VilenEera
发帖于2年前 1回/1K+阅
顶部