java与网络(概述)

1. 常用类

  • InetAddress
//使用getLocalHost方法创建InetAddress对象  本机
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.getHostAddress());  //返回:192.168.1.110
System.out.println(addr.getHostName());  //输出计算机名

//根据域名得到InetAddress对象
addr = InetAddress.getByName("www.beeworkshop.com"); 
System.out.println(addr.getHostAddress());  //返回服务器的ip:
System.out.println(addr.getHostName());  //输出:www.beeworkshop.com

//根据ip得到InetAddress对象
addr = InetAddress.getByName("123.56.78.9"); 
System.out.println(addr.getHostAddress());  //返回的ip:123.56.138.176
System.out.println(addr.getHostName());  //输出ip而不是域名。如果这个IP地 址不存在或DNS服务器不允许进行IP地址和域名的映射,

  • InetSocketAddress
//包含端口
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",9000);
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress2.getAddress());
System.out.println(socketAddress2.getPort());

2. 端口号

  • 0-1023 公认端口(分配给服务)
  • 1024-49151 注册端口(分配给用户进程或应用程序)
  • 49152-65535 动态私有端口

3. URL

URI = URL + URN
锚点 # 表示在同一页面或者同一网站内的跳转。

URL url = new URL("http://www.baidu.com:80/index.html?uname=shsxt&age=18#a");
//获取四个值
System.out.println("协议:"+url.getProtocol());
System.out.println("域名|ip:"+url.getHost());
System.out.println("端口:"+url.getPort());
System.out.println("请求资源1:"+url.getFile());
System.out.println("请求资源2:"+url.getPath());

//参数
System.out.println("参数:"+url.getQuery());
//锚点
System.out.println("锚点:"+url.getRef());

4. Web spider(网络爬虫)

  • 基本原理
//获取URL
URL url =new URL("https://www.jd.com");
//URL url =new URL("https://www.dianping.com");
//下载资源
InputStream is = url.openStream();
BufferedReader br =new BufferedReader(new InputStreamReader(is,"UTF-8"));
String msg =null;
while(null!=(msg=br.readLine())) {
	System.out.println(msg);
}
br.close();
//分析
//处理。。。。
  • 模拟浏览器
//获取URL
URL url =new URL("https://www.dianping.com");
//下载资源
HttpURLConnection  conn =(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
//User-Agent标识了我们的浏览器
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
BufferedReader br =new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String msg =null; 
while(null!=(msg=br.readLine())) {
	System.out.println(msg);
}
br.close();
//分析
//处理。。。。

你可能感兴趣的:(java)