zoukankan      html  css  js  c++  java
  • 【Java学习笔记】读取URL

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

    import java.net.URL;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    public class ReadUrl1 {
        public static void main(String arg[]) {
            String str;
            try {
    URL url = new URL("http://www.sohu.com/");
                InputStream is = url.openStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                while((str = br.readLine()) != null)
                    System.out.println(str);
                br.close();
            } catch(IOException e) {
                System.out.println(e);
            }
        }
    }

    另一种方法:

    import java.net.URL;
    import java.net.URLConnection;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    public class ReadUrl2 {
        public static void main(String arg[]) {
            String str;
            try {
                URL url = new URL("http://www.sohu.com/");
                URLConnection uc = url.openConnection();
                InputStream is = uc.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                while((str = br.readLine()) != null)
                    System.out.println(str);
                br.close();
            } catch(IOException e) {
                System.out.println(e);
            }
        }
    }

    还可以设置代理:

    import java.net.URL;
    import java.net.URLConnection;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.util.Properties;
    public class ReadUrl3 {
        public static void main(String arg[]) {
            String str;
            Properties props = System.getProperties();
            props.setProperty("proxySet","true");
            props.setProperty("proxyPort","8080");
            props.setProperty("proxyHost","proxy");
            try {
                URL url = new URL("http://www.sohu.com/");
                URLConnection uc = url.openConnection();
                InputStream is = uc.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                while((str = br.readLine()) != null)
                    System.out.println(str);
                br.close();
            } catch(IOException e) {
                System.out.println(e);
            }
        }
    }

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

  • 相关阅读:
    ZedBoard学习(6)System Generator实现串口通信(一行HDL代码都不用写)
    ZedBoard学习(1)Ubutun下进行串口通信
    Zedboard学习(7)PS下第一个裸奔程序
    激光雷达(一)数据采集C++
    win7/win8下安装Oracle1出错10g,提示“程序异常终止,发生未知错误”解决方法
    XML文件的加密与解密
    三层中最重要的SqlHelper类
    创建桌面快捷方式的语法
    秋招总结 艾尔夏尔
    thoughtworks二面准备 (三) 艾尔夏尔
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2822293.html
Copyright © 2011-2022 走看看