美文网首页
网络编程-demo

网络编程-demo

作者: Eve0 | 来源:发表于2017-09-14 21:12 被阅读0次

    • 访问baidu首页的html内容
    public class NetConnectionTest {
    
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://www.baidu.com");
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String res = "";
            
            while(null!=(res = reader.readLine())){
                System.out.println(res);
            }
            reader.close();
    
        }
    
    }
    
    • InetAddress
    public class NetConnectionTest {
    
        public static void main(String[] args) throws Exception {
            InetAddress ia = InetAddress.getLocalHost();//访问本机
            System.out.println(ia);
            InetAddress baidu = InetAddress.getByName("www.baidu.com");//通过域名访问
            System.out.println(baidu);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:网络编程-demo

          本文链接:https://www.haomeiwen.com/subject/uvncsxtx.html