Java中网络相关API的应用——InetAddress&URL

时间:2021-01-29 09:24:31

Java中网络相关API的应用——InetAddress&URL

一、InetAddress类

标识网络上的硬件资源

package com.homework;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays; //获取InetAddress类
public class TestInetAddress { public static void main(String[] args) throws UnknownHostException {
//获取本机的InetAddress实例
//InetAddress address=new InetAddress(); InetAddress类无法通过new来创建对象
InetAddress address=InetAddress.getLocalHost();
System.out.println(address);//获取本机的计算机名和IP地址
System.out.println("计算机名: "+address.getHostName());
System.out.println("IP地址: "+address.getHostAddress()); byte[] bytes=address.getAddress();
System.out.println("字节形式的IP: "+bytes);
System.out.println("字节数组形式的IP: "+Arrays.toString(bytes)); //根据机器名获取InetAddress实例
//InetAddress address2=InetAddress.getByName("MS-20150617TVTE");
InetAddress address2=InetAddress.getByName("192.168.0.176");
System.out.println("计算机名: "+address2.getHostName());
System.out.println("IP地址: "+address2.getHostAddress());
} }

结果

MS-20150717TVTE/192.168.0.176
计算机名: MS-20150617TVTE
IP地址: 192.168.0.176
字节形式的IP: [B@7852e922
字节数组形式的IP: [-64, -88, 0, -83]
计算机名: MS-20150617TVTE
IP地址: 192.168.0.176

二、URL(Uniform Resource Locator) :统一资源定位符

参数:protocol - 要使用的协议名称  host - 主机名称   port - 主机端口号   file - 主机上的文件   ref - URL 中的内部引用

package com.homework;

import java.net.MalformedURLException;
import java.net.URL; public class TestURL_01 {
public static void main(String[] args) {
try {
//创建一个URL实例
URL blogGarden=new URL("https://home.cnblogs.com");
        //根据已存在的url实例创建新的实例
//?后面表示参数,#后面表示锚点
URL url=new URL(blogGarden,"/index.html?username=tom#test");
System.out.println("协议: "+url.getProtocol());
System.out.println("主机: "+url.getHost());
//如果未指定端口号,则使用默认的端口号,此时getPort()方法返回值为-1
System.out.println("端口: "+url.getPort());
System.out.println("文件路径: "+url.getPath());
System.out.println("文件名: "+url.getFile());
System.out.println("相对路径: "+url.getRef());
System.out.println("查询字符串: "+url.getQuery()); } catch (MalformedURLException e) {
e.printStackTrace();
} }
}

结果:

协议:  https
主机: home.cnblogs.com
端口: -1
文件路径: /index.html
文件名: /index.html?username=tom
相对路径: test
查询字符串: username=tom

三、通过URL读取网页上的内容

     Java中网络相关API的应用——InetAddress&URL

package com.homework;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL; //通过URL读取网页上的内容
public class TestURL_02 {
public static void main(String[] args) {
try {
//创建一个url实例
URL url=new URL("http://www.baidu.com");
//通过URL的openStream方法获取URL对象所表示的资源的字节输入流
InputStream is=url.openStream();
//将其转换为字符输入流
InputStreamReader isr=new InputStreamReader(is,"utf-8");
//为字符流添加缓冲
BufferedReader br=new BufferedReader(isr);
String data=br.readLine();//读取数据 while(data!=null){//循环读取数据
System.out.println(data); //输出数据
data=br.readLine();
}
br.close();
isr.close();
is.close(); } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } }

结果:百度页面的编码

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge>
<meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css>
<title>百度一下,你就知道</title></head> <body········································· ·····················</body>