明耀

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
import java.net.*;
import java.io.*;
public class URLDemo {
    public static void main(String[] args)
    {
        try
        {
            //利用Java.net中的URL类来获取资源
            URL url=new URL("https://www.baidu.com");
            URLConnection urlConnection=url.openConnection();
            HttpURLConnection connection=null;
            if(urlConnection instanceof HttpURLConnection)
                connection=(HttpURLConnection)urlConnection;
            else
            {
                System.out.println("请输入有效的URL地址");
                return;
            }
            InputStreamReader isr=new InputStreamReader(connection.getInputStream(),"UTF-8");
            BufferedReader in=new BufferedReader(isr);
            String current;
            while((current=in.readLine())!=null)
            {
                System.out.println(current);
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

}

 

posted on 2017-09-01 20:04  明耀  阅读(1530)  评论(0编辑  收藏  举报