zoukankan      html  css  js  c++  java
  • Java网络编程之InetAddress和URL

    在Java中提供了专门的网络开发程序包---java.net,java的网络编程提供了两种通信协议:TCP(传输控制协议)和UDP(数据报协议)。

    一.IP(Internet Protocol) 与InetAddress类

    1.IP简介

    互联网上的每一台计算机都有一个唯一表示自己的标识,即IP地址。

    IP地址=网络地址+主机地址

    2.InetAddress

    该类主要表示IP地址,有两个子类:Inet4Address、Inet6Address,前者表示IPV4,后者表示IPV6。

    InetAddress类的常用方法有:

    类型 方法 描述
    static InetAddress getByName(Stringhost) 在给定主机名的情况下确定主机的 IP 地址。
    static InetAddress

    getLocalHost()

    返回本地主机。
    String getHostName() 获取此 IP 地址的主机名。
    boolean isReachable(int timeout) 测试是否可以达到该地址。
    测试InetAddress类:
    package org.demo.net;
    
    import java.net.InetAddress;
    /**
     * 测试InetAddress类
     * @author oushine
     */
    public class InetAddressDemo {
        public static void main(String[] args) {
            try {
                //声明并得到本地InetAddress对象
                InetAddress iAddress1=InetAddress.getLocalHost();
                //声明并得到远程InetAddress对象
                InetAddress iAddress2=InetAddress.getByName("www.baidu.com");
                //获得本地IP地址
                System.out.println("本机IP地址为:"+iAddress1.getHostAddress());
                //获得远程IP地址
                System.out.println("百度的IP地址是:"+iAddress2.getHostAddress());
                System.out.println("本机是否可达:"+iAddress1.isReachable(3000));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }

    结果:

    1111

    二.URL与URLConnection

    1.URL

    URL(Uniform Resource Locator)是统一资源定位符,可以直接使用此类找到互联网上的资源(比如一个网页)。

    URL类常用方法:

    类型 方法 描述
    构造方法

    URL(String spec)

    根据 String 表示形式创建 URL 对象。
    构造方法 URL(String protocol, String host, int port, String file) 根据指定 protocolhostport 号和 file 创建 URL 对象。
    URLConnection openConnection() 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
    InputStream

    openStream()

    打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream
    使用URL读取内容:
    package org.demo.net;
    
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Scanner;
    
    public class UrlDemo {
        public static void main(String[] args) {
            URL url;
            try {
                //指定操作的URL
                url = new URL("http","www.baidu.com",80,"/index.html");
                //打开输入流,读取URL内容
                InputStream inputStream=url.openStream();
                Scanner scan=new Scanner(inputStream);
                //设置读取分隔符
                scan.useDelimiter("
    ");
                while(scan.hasNext()){
                    //输出内容
                    System.out.println(scan.next());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    结果:

    2222

    显示出来的是HTML代码。

    2.URLConnection

    URLConnection是封装访问远程网络资源一般方法的类,通过它可以建立与远程服务器的连接,检查远程资源的一些属性。

    常用方法:

    类型 方法 描述
    int

    getContentLength()

    返回 content-length 头字段的值。
    String

    getContentType()

    返回 content-type 头字段的值。
    InputStream

    getInputStream()

    返回从此打开的连接读取的输入流。

    URLConnection对象可以通过URL类的openConnection()方法取得。

    取得URL的基本信息:
    package org.demo.net;
    
    import java.net.URL;
    import java.net.URLConnection;
    
    public class URLConnectionDemo {
        public static void main(String[] args) {
            try {
                URL url=new URL("http://www.baidu.com");
                //建立连接
                URLConnection urlConn=url.openConnection();
                System.out.println("内容大小:"+urlConn.getContentLength());
                System.out.println("内容类型:"+urlConn.getContentType());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    运行结果:
    3333
     

    三.URLEncoder与URLDecoder

    在java中如果需要完成编码和解码操作就要使用URLEncoder和URLDecoder两个类。

    URLEncoder类的方法:

    类型 方法 描述
    static String encode(String s, String enc) 使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式。

    URLDecoder类的方法:

    类型 方法 描述
    static String decode(String s, String enc) 使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。
    编码及解码操作:
    package org.demo.net;
    
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    
    public class CodeDemo {
        public static void main(String[] args) {
            String keyWord="oushine 阳";
            try {
                String enCode=URLEncoder.encode(keyWord, "UTF-8");
                System.out.println("编码之后:"+enCode);
                String deCode=URLDecoder.decode(enCode, "UTF-8");
                System.out.println("解码之后:"+deCode);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
    
    }

    运行结果:

    5555

    转自:https://www.cnblogs.com/yzl-i/p/4442892.html#top

  • 相关阅读:
    Git代码托管
    Git安装配置
    【知识共享】SSIS的典型应用
    【转贴】正确实施BI项目的四条建议
    SAP ABAP 常用事务代码
    【转贴】WP7 离线升级方法附错误解决方案
    BO Universe(语义层)设计相关
    【转贴】SAP学习经验谈
    七个不放过和四项原则
    SAP BO帮助文档下载页面
  • 原文地址:https://www.cnblogs.com/yixiu868/p/8001646.html
Copyright © 2011-2022 走看看