勿以恶小而为之,勿以善小而不为--------------------------刘备
劝诸君,多行善事积福报,莫作恶
上一章简单介绍了 多线程的锁(六),如果没有看过,请观看上一章
网络编程,是很重要的,一定要掌握的。
关于网络编程,需要掌握 InetAddress 和 InetSocketAddress 等类。
一. InetAddress 类
InetAddress 是将主机名与ip地址进行关联的类, 指的是哪一个主机。
一.一 方法
一.一.一 构造方法
一.一.一.一 方法
方法 | 作用 |
---|---|
getByName(String host) | 根据主机名或者ip获取主机 |
getLocalHost() | 获取本机 |
一.一.一.二 演示
@Test
public void conTest(){
//1. 根据主机名获取
try {
InetAddress inetAddress=InetAddress.getByName("www.baidu.com");
System.out.println("输出ip:"+inetAddress.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
// 百度的 ip 地址
try {
InetAddress inetAddress1=InetAddress.getByName("39.156.66.14");
System.out.println("输出名称:"+inetAddress1.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//获取当地的
try {
//等价于 localhost ,获取的并不是 127.0.0.1, 而是ipconfig的值
InetAddress inetAddress2=InetAddress.getLocalHost();
System.out.println("名称:"+inetAddress2.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
运行程序,控制台打印输出:
一.一. 二 其他常用方法
方法 | 作用 |
---|---|
String getHostAddress() | 获取主机 |
boolean isReachable(int timeout) | 指定时间内是否可达 |
一.二 演示 InetAddress
@Test
public void methodTest() throws Exception{
//获取当地的
InetAddress inetAddress=InetAddress.getLocalHost();
System.out.println("输出主机名:"+inetAddress.getHostName());
System.out.println("输出主机地址:"+inetAddress.getHostAddress());
System.out.println("完全限定域名:"+inetAddress.getCanonicalHostName());
//在指定的时间内是否可达
System.out.println("判断是否可达:"+inetAddress.isReachable(200));
//yuezl/192.168.190.1
System.out.println("ip地址:"+inetAddress.toString());
}
运行程序,控制台打印输出:
二. InetSocketAddress 类
指出 具体的哪个主机的哪个端口
二.一 方法
二.一.一 构造方法
二.一.一.一 方法
方法 | 作用 |
---|---|
InetSocketAddress(String hostname, int port) | 传入主机名和端口号 |
InetSocketAddress(InetAddress addr, int port) | 传入 inetAddress对象和端口号 |
静态方法获取对象 | |
static InetSocketAddress createUnresolved(String host, int port) | 从主机名和端口号创建未解析的套接字地址。 |
二.一.一.二 演示
@Test
public void conTest(){
//放置主机名和端口
InetSocketAddress inetSocketAddress=new InetSocketAddress("localhost",999);
//第二种方式
InetAddress inetAddress= null;
try {
inetAddress = InetAddress.getByName("localhost");
} catch (UnknownHostException e) {
e.printStackTrace();
}
InetSocketAddress inetSocketAddress1=new InetSocketAddress(inetAddress,999);
//3. 通过静态方法获取
InetSocketAddress inetSocketAddress2=InetSocketAddress.createUnresolved("localhost",999);
}
二.一.二 其他常用方法
方法 | 作用 |
---|---|
String getHostName() | 获取主机名 |
int getPort() | 获取端口号 |
InetAddress getAddress() | 获取 inetAddress对象 |
二.二 演示 InetSocketAddress
@Test
public void methodTest() throws Exception{
//1. 实例化本地的
InetSocketAddress inetSocketAddress=new InetSocketAddress("localhost",999);
//.获取主机名
System.out.println("主机名:"+inetSocketAddress.getHostName());
System.out.println("获取端口:"+inetSocketAddress.getPort());
System.out.println("获取 hostString:"+inetSocketAddress.getHostString());
InetAddress inetAddress=inetSocketAddress.getAddress();
System.out.println("判断是否可达:"+inetAddress.isReachable(200));
}
运行程序,控制台打印输出
三. URL 类
指定网络的具体的地址, 可以带请求参数和描点,能够精确的定位到地址。
三.一 方法
三.一.一 构造方法
三.一.一.一 方法
方法 | 作用 |
---|---|
URL(String spec) | 直接传入 url 字符串地址 |
URL(String protocol, String host, int port, String file) | 传入协议,主机名,端口和具体的地址 |
三.一.一.二 演示
@Test
public void conTest(){
try {
//1. 第一种方式
URL url=new URL("https://blog.csdn.net/yjltx1234csdn/article/details/105459342");
} catch (MalformedURLException e) {
e.printStackTrace();
}
//2. 第二种
try {
URL url1=new URL("https","blog.csdn.net",80,"yjltx1234csdn/article/details/105459342");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
三.一.二 其他方法
方法 | 作用 |
---|---|
String getProtocol() | 获取协议名称 |
String getHost() | 获取主机 |
int getPort() | 获取端口号,如果传入了,则能获取到,没有传入,则返回 -1 |
int getDefaultPort() | 获取默认的端口号,如 80,21,443等 |
String getFile() | 获取完整的路径名 |
String getPath() | 只获取路径部分 |
String getQuery() | 获取查询参数部分 |
String getRef() | 获取描点 |
文件流部分: | |
InputStream openStream() | 将该文件变成输入流的形式 |
爬虫部分: | |
URLConnection openConnection() | 变成 URLConnection 部分 |
三.二 演示 URL
三.二.一 RESTFUL 的地址形式
@Test
public void operTest() throws Exception{
//1. 构造对象, restful 的形式
URL url=new URL("https://blog.csdn.net/yjltx1234csdn/article/details/105459342");
System.out.println("获取协议:"+url.getProtocol());
System.out.println("获取主机:"+url.getHost());
System.out.println("获取端口:"+url.getPort());
System.out.println("获取默认端口:"+url.getDefaultPort());
System.out.println("获取地址:"+url.getFile());
//变成流,读取相应的文件信息. 注意,不一定所有的url都可以进行转换。
InputStream inputStream=url.openStream();
int len=-1;
byte[] bytes=new byte[1024];
StringBuilder sb=new StringBuilder();
while((len=inputStream.read(bytes))!=-1){
String temp=new String(bytes,0,len);
sb.append(temp);
}
System.out.println("文件内容是:\n"+sb.toString());
//打开connection,关于 urlConnection,后面会讲解
URLConnection connection=url.openConnection();
}
运行程序,查看控制台输出:
能够查询出文件的内容信息。
三.二.二 正常的ur地址
@Test
public void oper1Test() throws Exception{
URL url=new URL("https://www.baidu.com:80/TwoButterfly/index.html?uname=两个蝴蝶飞&age=24#a");
System.out.println("获取路径path:"+url.getPath()); ///TwoButterfly/index.html
System.out.println("获取查询部分:"+url.getQuery()); //uname=两个蝴蝶飞&age=24
System.out.println("获取描点:"+url.getRef()); //a
System.out.println("获取userInfo:"+url.getUserInfo());//null
}
运行程序,查看控制台输出:
四. URLConnection类
程序与 url 进行通信, 是一个非常重要的类。
用 URLConnection 类,主要可以获取网络地址 url 的相应的信息, 可以模拟浏览器的访问,达到爬虫的目的。
四.一 方法
四.一.一 构造方法
四.一.一.一 方法
方法 | 作用 |
---|---|
URLConnection openConnection() | 变成 URLConnection 部分 |
四.一.一.二 演示
@Test
public void conTest() throws Exception{
URL url=new URL("https://blog.csdn.net/yjltx1234csdn/article/details/105459342");
//url 获取的方法
URLConnection urlConnection=url.openConnection();
}
四.一.二 其他方法
方法 | 作用 |
---|---|
String getContentEncoding() | 获取编码方式 |
String getContentType() | 获取 content-type的值 |
void setConnectTimeout(int timeout) | 设置连接超时时间 |
void setRequestProperty(String key, String value) | 设置请求访问的属性,模拟浏览器访问 |
InputStream getInputStream() | 获取输入流 |
OutputStream getOutputStream() | 获取输出流 |
四.二 演示URLConnection 爬虫
四.二.一 爬取没有设置禁止爬虫的网址
@Test
public void pa1Test() throws Exception{
URL url=new URL("https://www.jd.com");
// URL url=new URL("https://www.dianping.com");
InputStream inputStream=url.openStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String temp=null;
while(null!=(temp=bufferedReader.readLine())){
System.out.println(temp);
}
//关闭流
bufferedReader.close();
inputStream.close();
}
运行程序,查看控制台输出
如果不允许爬虫,如 dianping 网的话,那么会抛出异常
java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.dianping.com
四.二.二 模拟浏览器进行访问爬取
@Test
public void pa2Test() throws Exception{
URL url=new URL("https://www.dianping.com");
//模拟浏览器,因为浏览器可以访问
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
//设置请求方式
httpURLConnection.setRequestMethod("GET");
//设置时间
httpURLConnection.setReadTimeout(2000);
//设置浏览器
httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/77.0");
//设置语言
httpURLConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,
en-US;q=0.3,en;q=0.2");
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String temp=null;
while(null!=(temp=bufferedReader.readLine())){
System.out.println(temp);
}
//关闭流
bufferedReader.close();
inputStream.close();
}
运行程序,查看控制台输出打印
谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!