InetAddress类与URL

import java.net.*;
import java.io.*;
public class InetAdressTess {
    public static void main(String []args) throws IOException {
        
        InetAddress ip=InetAddress.getLocalHost();
        InetAddress ip1=InetAddress.getByName("www.baidu.com");
        InetAddress ip2=InetAddress.getByAddress(ip.getAddress());
         System.out.println("获得主机ip地址:"+ip1.getHostAddress());
        System.out.println("从域名服务中获得标准主机名:"+ip.getCanonicalHostName());
        System.out.println("获得主机ip地址:"+ip.getHostAddress());
        System.out.println("获得主机名:"+ip.getHostName());
        System.out.println("获得主机名和ip地址字符串:"+ip.toString());
        URL baidu=new URL("http://www.baidu.com/");
        //获取输入流,构造一个BufferedReader对象
        //获取指点网址资源的html源码
        BufferedReader br=new BufferedReader(new InputStreamReader(baidu.openStream()));
        String inputLine;
        while((inputLine=br.readLine())!=null)
            System.out.println(inputLine);
        br.close();
       
        
    }
}
//一个合法的url:如:    http://www.baidu.com:8080/index.htm
//由四个部分组成:         协议名+主机名+端口+文件路径

猜你喜欢

转载自blog.csdn.net/luoshiyong123/article/details/80489792