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 InetAddressgetByName(Stringhost)在给定主机名的情况下确定主机的 IP 地址。
static InetAddress

getLocalHost()

返回本地主机。
StringgetHostName()获取此 IP 地址的主机名。
booleanisReachable(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 对象。
URLConnectionopenConnection()返回一个 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("\n");
            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();
        }
    }

}
运行结果:
 

三.URLEncoder与URLDecoder

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

URLEncoder类的方法:

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

URLDecoder类的方法:

类型方法描述
static Stringdecode(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

转载于:https://www.cnblogs.com/yixiu868/p/8001646.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值