Java中网络相关API

InetAddress:用于标识网络上的硬件资源,即表示IP地址。

/*
 * InetAddress类
 */
public class Test01 {

    public static void main(String[] args) throws UnknownHostException {
        // 获取本机的InetAddress实例
        InetAddress address = InetAddress.getLocalHost();
        System.out.println("计算名:" + address.getHostName());
        System.out.println("IP地址:" + address.getHostAddress());
        byte[] bytes = address.getAddress();// 获取字节数组形式的IP地址
        System.out.println("字节数组形式的IP:" + Arrays.toString(bytes));
        System.out.println(address);// 直接输出InetAddress对象

        // 根据机器名获取InetAddress实例
        // InetAddress address2 = InetAddress.getByName("NJZBXXNB015");
        InetAddress address2=InetAddress.getByName("192.168.199.1");
        System.out.println("计算名:" + address2.getHostName());
        System.out.println("IP地址:" + address2.getHostAddress());
    }

}

  

URL类介绍

* Uniform Resource Locator 统一资源定位符,标示Internet上某一资源的地址.

* 由协议名和资源名称组成,中间用冒号隔开.

* java.net包中有URL类. * url中?后表示参数,#表示锚点.

* url若未指定端口号,则使用默认端口号,同时getPort()方法返回-1.

* 相对路径是锚点内容.

/*
 * URL常用方法
 */
public class Test02 {
    public static void main(String[] args) {
        try {
            //创建一个URL实例
            URL imooc = new URL("http://www.imooc.com");
            //?后面表示参数,#后面表示锚点
            URL url = new URL(imooc, "/index.html?username=tom#test");
            System.out.println("协议:" + url.getProtocol());
            System.out.println("主机:" + url.getHost());
            //如果未指定端口号,则使用默认的端口号,此时getPort()方法返回值为-1
            System.out.println("端口:" + url.getPort());
            System.out.println("文件路径:" + url.getPath());
            System.out.println("文件名:" + url.getFile());
            System.out.println("相对路径:" + url.getRef());
            System.out.println("查询字符串:" + url.getQuery());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

使用URL读取网页内容

1. 通过URL对象的openStream()方法可以得到指定的资源输入流.

2. 通过输入流可以读取,访问网络上的数据.

  * 获取InputStream字节输入流对象,用InputStreamReader转化为字符输入流,同时可以设置字符集.

  * 用BufferedReader为其添加缓冲,然后读取数据.

/*
 * 使用URL读取网页内容
 */
public class Test03 {
    public static void main(String[] args) {
        try {
            //创建一个URL实例
            URL url = new URL("http://www.baidu.com");
            //通过URL的openStream方法获取URL对象所表示的资源的字节输入流
            InputStream is = url.openStream();
            //将字节输入流转换为字符输入流
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            //为字符输入流添加缓冲
            BufferedReader br = new BufferedReader(isr);
            String data = br.readLine();//读取数据
            while (data != null) {//循环读取数据
                System.out.println(data);//输出数据
                data = br.readLine();
            }
            br.close();
            isr.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/ooo0/p/12243272.html