网络编程-URL

import java.net.*;
class  URLDemo
{
    public static void main(String[] args) throws MalformedURLException
    {
        URL url = new URL("https://www.baidu.com/");
        System.out.println("获取URL的协议名称getProtocol:"+url.getProtocol());
        System.out.println("获取此URL的主机名getHost:"+url.getHost());
        System.out.println("获取此URL的端口号getPort:"+url.getPort());
        System.out.println("获取此URL的路劲部分getPath:"+url.getPath());
        System.out.println("获取此URL的文件名getFile:"+url.getFile());
        System.out.println("获取此URL的查询部getQuery:"+url.getQuery());
    }
}

网络编程-URL_第1张图片

读取百度页面信息

import java.net.*;
import java.io.*;
class  URLConnection
{
    public static void main(String[] args) throws Exception
    {
        URL url = new URL("https://www.baidu.com/");

        URLConnection conn = url.openConnection();

        BufferedReader read = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));

        String line = null;
        while((line =read.readLine())!=null)
        {               
            System.out.println(line);
        }
    }
}

域名解析

想要将主机名,如(www.baidu.com)翻译成ip地址,需要域名解析,DNS
网络编程-URL_第2张图片

可以走DNS,也可以在本地主机找映射关系
网络编程-URL_第3张图片

你可能感兴趣的:(Java,SE)