码迷,mamicode.com
首页 > 编程语言 > 详细

Java根据一个网址链接获取源代码

时间:2015-04-09 17:14:29      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpTest {
    private String u;
    private String encoding;

    public static void main(String[] args) throws Exception {
        HttpTest client = new HttpTest("http://www.baidu.com/", "UTF-8");
        client.run();
    }

    public HttpTest(String u, String encoding) {
        this.u = u;
        this.encoding = encoding;
    }

    public void run() throws Exception {

        URL url = new URL(u);// 根据链接(字符串格式),生成一个URL对象

        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();// 打开URL

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                urlConnection.getInputStream(), encoding));// 得到输入流,即获得了网页的内容
        String line; // 读取输入流的数据,并显示
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
}


 

Java根据一个网址链接获取源代码

标签:

原文地址:http://www.cnblogs.com/mini-firework/p/4409987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!