免费看欧美黑人毛片-免费看毛片网站-免费看毛片的网站-免费看久久-中文字幕日韩欧美-中文字幕日韩精品一区

您現(xiàn)在的位置:首頁 > IT認(rèn)證 > JAVA >

Java基礎(chǔ)知識:java獲取網(wǎng)頁源代碼


要分析某個網(wǎng)頁中的代碼構(gòu)成,需要某個結(jié)點下的內(nèi)容。用此原始方法可以得到整個網(wǎng)頁的源碼。其實更簡單的方法是使用 WebClient 或 HtmlUtil 等開源方式 。

[java]  

public class HtmlParser {  

    public static String getHtmlContent(URL url, String encode) {  

        StringBuffer contentBuffer = new StringBuffer();  

  

        int responseCode = -1;  

        HttpURLConnection con = null;  

        try {  

            con = (HttpURLConnection) url.openConnection();  

            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// IE代理進行下載  

            con.setConnectTimeout(60000);  

            con.setReadTimeout(60000);  

            // 獲得網(wǎng)頁返回信息碼  

            responseCode = con.getResponseCode();  

            if (responseCode == -1) {  

                System.out.println(url.toString() + " : connection is failure...");  

                con.disconnect();  

                return null;  

            }  

            if (responseCode >= 400) // 請求失敗  

            {  

                System.out.println("請求失敗:get response code: " + responseCode);  

                con.disconnect();  

                return null;  

            }    InputStream inStr = con.getInputStream();  

            InputStreamReader istreamReader = new InputStreamReader(inStr, encode);  

            BufferedReader buffStr = new BufferedReader(istreamReader);  

  

            String str = null;  

            while ((str = buffStr.readLine()) != null)  

                contentBuffer.append(str);  

            inStr.close();  

        } catch (IOException e) {  

            e.printStackTrace();  

            contentBuffer = null;  

            System.out.println("error: " + url.toString());  

        } finally {  

            con.disconnect();  

        }  

        return contentBuffer.toString();  

    }  

  

    public static String getHtmlContent(String url, String encode) {  

        if (!url.toLowerCase().startsWith("")) {  

            url = "" + url;  

        }  

        try {  

            URL rUrl = new URL(url);  

            return getHtmlContent(rUrl, encode);  

        } catch (Exception e) {  

            e.printStackTrace();  

            return null;  

        }  

    }  

    public static void main(String argsp[]){  

        System.out.println(getHtmlContent("","utf-8")) ;  

          

    }  

}  

相關(guān)文章

無相關(guān)信息
更新時間2022-09-16 10:07:22【至頂部↑】
聯(lián)系我們 | 郵件: | 客服熱線電話:4008816886(QQ同號) | 

付款方式留言簿投訴中心網(wǎng)站糾錯二維碼手機版

電話:
付款方式   |   給我留言   |   我要糾錯   |   聯(lián)系我們




主站蜘蛛池模板: 电影宝贝| 我,喜欢你演员表介绍| 镇魂街第三季| 戚薇床戏| 奥特曼格斗进化重生破解版| 追踪 电影| 凤凰卫视节目表| 想太多吉他谱| av电影网| 跳跳动画| 翟小兴| 柏欣彤广场舞| 朴智勋| 《感恩的心》儿童朗诵| 张勇手演过的电影| 乱世佳人电视剧免费观看完整版| 傅韵哲| 哥斯| stylistic device| 1905电影网免费电影| 天天快乐视频免费观看下载| 麦当娜·西科尼| 别说我的眼泪你无所谓吉他谱| 美女网站视频免费| 骆文博| 姬培杰| 包公大战潘金莲| 安息2| 浙江卫视回放观看入口| 尼基塔 电影| 投诉法官最快最有效果电话| 心花路放 豆瓣| 古诗改编版搞笑大全| 兰陵王电影| 境界bleach千年血战| 年轻阿姨的性教育| 好看电影视频| 单身即地狱| 皇家骑士团外传| 解毒咒| 张小波简历及个人资料简介|