一. InetAddress
- IP 地址是 IP 使用的 32 位或 128 位无符号数字,它是一种低级协议,UDP 和 TCP 协议都是在它的基础上构建的。
-
byte[] getAddress()
返回此 InetAddress 对象的原始 IP 地址。 -
static InetAddress getByAddress(byte[] addr)
在给定原始 IP 地址的情况下,返回 InetAddress 对象。 -
static InetAddress getByName(String host)
在给定主机名的情况下确定主机的 IP 地址。 -
String getHostAddress()
返回 IP 地址字符串(以文本表现形式)。 -
static InetAddress getLocalHost()
返回本地主机。 -
boolean isReachable(NetworkInterface netif, int ttl, int timeout)
测试是否可以达到该地址。
新建类InetAddressDemo
//InetAddress类
public class InetAddressDemo {
public static void main(String[] args) throws Exception {
//返回主机IP对象,需要抛出不知道主机异常,UnknownHost
InetAddress locAdd = InetAddress.getLocalHost();
//返回指定主机名地址IP对象
InetAddress remAdd = InetAddress.getByName("www.tabao.com");
//创建ip地址数组
byte[] ip = {(byte)112,90,32,14};
//getByAddress(),返回此InetAddress对象的原始IP地址
InetAddress remAdd2 = InetAddress.getByAddress(ip);
//返回IP地址字符串
System.out.println("本机IP = " + locAdd.getHostAddress());
//
System.out.println("淘宝 = " + remAdd.getHostAddress());
//是否可以送达,需要抛出IO异常,IO
System.out.println("是否可以送达: " + remAdd2.isReachable(2000));
}
}
二. URL
构造方法
- URL(String spec):根据指定的地址实例化URL对象
- URL(String protocol,String host,int port,String file)
常用方法
- public URLConnection openConnection():得到URLConnection对象
- public final InputStream openStream():得到输入流
新建类URLDemo
public class URLDemo {
public static void main(String[] args) throws IOException {
//http://sports.qq.com/a/20150716/027613.htm
//需要MalformedURLException抛出这一异常指示出现了错误的 URL
URL url = new URL("http","sports.qq.com",80,"/a/20150716/027613.htm");
//得到输入流
InputStream is = url.openStream();
//获取Scanner对象
Scanner sc = new Scanner(is);
//分隔
sc.useDelimiter("\r\n");
//遍历
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
}
}
三. URLConnection
常用方法
- public int getContentLength():获取内容长度
- public String getConntentType():取得内容类型
- public InpurStream getInputStream():获取连接的输入流
- public void setRequestProperty(String key,String value):设置一般请求属性
- public String getHeaderField(String name):返回指定的头字段的值
新建类URLConnectionDemo
//URLConnectionDemo
public class URLConnectionDemo {
public static void main(String[] args) throws IOException {
//http://sports.qq.com/a/20150716/027613.htm
URL url = new URL("http","sports.qq.com",80,"a/20150716/027613.htm");
//得到URLConnection对象,需要抛出IO异常
URLConnection uc = url.openConnection();
//获取内容长度
System.out.println("内容长度 = " + uc.getContentLength());
//获取内容类型
System.out.println("内容类型 = " + uc.getContentType());
}
}
四. URLEncoder和URLDecoder
- static String encode(String s, String enc)
使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式。 - static String decode(String s, String enc)
使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。
新建类URLEncoderDemo
//编码,解码
public class URLEncoderDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
//使用指定编码机制将字符串转码 需要抛UnsupportedEncodingException
String str = URLEncoder.encode("huangkun18","UTF-8");
System.out.println("编码后 : " + str);
//使用指定编码机制解码
str = URLDecoder.decode(str,"UTF-8");
System.out.println("解码后 : " + str);
}
}