1.InetAdress的使用
此类表示互联网协议 (IP) 地址。
InetAddress对象需要通过它提供的静态方法来获取
static InetAddress[] getAllByName(String host)
在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
static InetAddress getByAddress(byte[] addr)
在给定原始 IP 地址的情况下,返回 InetAddress 对象。
static InetAddress getByAddress(String host,byte[] addr)
根据提供的主机名和 IP 地址创建 InetAddress。
static InetAddress getByName(String host)
在给定主机名的情况下确定主机的 IP 地址。
static InetAddress getLocalHost()
返回本地主机。
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
public static void main(String[] args) throws UnknownHostException, UnsupportedEncodingException {
// 以www.baidu.com 为例
String host = "www.baidu.com";
byte[] addr = new byte[] {};
InetAddress[] mAddresses;
InetAddress mAddress;
// InetAddress[] getAllByName(String host)在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
mAddresses = InetAddress.getAllByName(host);
for (InetAddress inetAddress : mAddresses) {
System.out.println(inetAddress);
}
// InetAddress getByName(String host)在给定主机名的情况下确定主机的 IP 地址。
mAddress = InetAddress.getByName(host);
System.out.println(mAddress);
printInfo(mAddress);
// 返回此 InetAddress 对象的原始 IP 地址。
addr = mAddress.getAddress();
// InetAddress getByAddress(byte[] addr)在给定原始 IP 地址的情况下,返回 InetAddress 对象。
mAddress = InetAddress.getByAddress(addr);
printInfo(mAddress);
// InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。
mAddress = InetAddress.getByAddress(host, addr);
printInfo(mAddress);
// InetAddress getLocalHost() 返回本地主机。
mAddress = InetAddress.getLocalHost();
printInfo(mAddress);
}
public static void printInfo(InetAddress inetAddress) {
System.out.println("完全限定名:" + inetAddress.getCanonicalHostName());
System.out.println("IP 地址字符串:" + inetAddress.getHostAddress());
System.out.println("IP 地址的主机名:" + inetAddress.getHostName());
System.out.println();
}
}
控制台输出
www.baidu.com/220.181.112.244
www.baidu.com/220.181.111.188
www.baidu.com/220.181.112.244
完全限定名:220.181.112.244
IP 地址字符串:220.181.112.244
IP 地址的主机名:www.baidu.com
完全限定名:220.181.112.244
IP 地址字符串:220.181.112.244
IP 地址的主机名:220.181.112.244
完全限定名:220.181.112.244
IP 地址字符串:220.181.112.244
IP 地址的主机名:www.baidu.com
完全限定名:LaiYaXing
IP 地址字符串:192.168.2.101
IP 地址的主机名:LaiYaXing
2.URL的使用
类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class URLTest {
static URL url;
public static void main(String[] args) throws IOException {
/*
查看URL的源码
public URL(String spec) throws MalformedURLException {
this(null, spec);
}
public URL(URL context, String spec) throws MalformedURLException {
this(context, spec, null);
}
public URL(URL context, String spec, URLStreamHandler handler)
查看源码得知最终URL(String spec)会调用URL(URL context, String spec, URLStreamHandler handler)
此时此时context和handler为null。
在构造函数URL(URL context, String spec, URLStreamHandler handler)中
如果handler为空会调用 handler = getURLStreamHandler(protocol)的方法根据protocol协议初始化handler
handler为URLStreamHandler的子类实例
*/
//构造方法URL(String spec)
url = new URL("http://www.baidu.com");
printUrlInfo(url);
/*
URLConnection openConnection()返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
public URLConnection openConnection() throws java.io.IOException {
return handler.openConnection(this);
}
查看源码可以得知openConnection()返回了handler.openConnection(this);
在构造方法中URL(URL context, String spec, URLStreamHandler handler)已经根据protocol协议自动初始化了handler = getURLStreamHandler(protocol)
handler.openConnection(this);返回的是URLConnection子类的实例。所以最好把URLConnection转化
*/
//HttpURLConnection conn=(HttpURLConnection) url.openConnection();
URLConnection conn=url.openConnection();
/*
InputStream openStream() 打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
public final InputStream openStream() throws java.io.IOException {
return openConnection().getInputStream();
}
查看源码可以得知openStream()是先调用了openConnection方法得到一个URLConnection然后再得到URLConnection实例的输入流
*/
InputStream is = url.openStream();
//获取输入流内容
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String result="";
String Line = null;
while ((Line=br.readLine())!=null) {
result=result+Line;
}
System.out.println(result);
}
//输出URL信息
public static void printUrlInfo(URL url) {
System.out.println("Url:"+url);
System.out.println("Protocol:"+url.getProtocol());
System.out.println("Host:"+url.getHost());
System.out.println("Port"+url.getPort());
System.out.println("Path:"+url.getPath());
System.out.println("File:"+url.getFile());
System.out.println();
}
}