目录
IP(InetAddress)
端口(InetSocketAddress)
URL
传输协议
UDP
TCP
爬虫基本原理">网络编程应用-->爬虫基本原理
IP(InetAddress)
package net; import java.net.InetAddress; public class IpTest { public static void main(String[] args) throws Exception { //使用getLocalHost获取本机的InetAddress对象 InetAddress address = InetAddress.getLocalHost(); System.out.println(address.getHostAddress()); System.out.println(address.getHostName()); //根据指定的域名获取相应的InetAddress对象 address = InetAddress.getByName("www.baidu.com"); System.out.println(address.getHostAddress()); System.out.println(address.getHostName()); //根据指定IP获取相应的InetAddress对象 address = InetAddress.getByName("127.0.0.1"); System.out.println(address.getHostAddress()); System.out.println(address.getHostName()); } }端口(InetSocketAddress)
package net; import java.net.InetSocketAddress; public class PortTest { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080); InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress2.getHostName()); System.out.println(socketAddress2.getAddress()); } }URL
package net; import java.net.MalformedURLException; import java.net.URL; public class UrlTest { public static void main(String[] args) throws MalformedURLException { URL url = new URL("https://www.baidu.com/s?wd=%E7%99%BE%E5%BA%A6#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("请求资源1:" + url.getPath()); System.out.println("参数:" + url.getQuery()); System.out.println("锚点:" + url.getRef()); } }传输协议 UDP
package net.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; public class UdpClientTest { public static void main(String[] args) throws IOException { System.out.println("发送端启动。。。"); //1、使用DatagramSocket,指定端口,创建发送端 DatagramSocket ds = new DatagramSocket(55000); //2、准备数据,必须转换为字节数组 String data = "Hello world"; byte[] datas = data.getBytes(); //3、封装成DatagramPacket包裹,并指定目的地 DatagramPacket dp = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 56000)); //4、发送包裹send ds.send(dp); //5、释放资源 ds.close(); System.out.println("发送端关闭。。。"); } }
package net.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UdpServerTest { public static void main(String[] args) throws IOException { System.out.println("接收端启动。。。"); //1、使用DatagramSocket,指定端口,创建接收端 DatagramSocket ds = new DatagramSocket(56000); //2、准备容器,封装成DatagramPacket包裹 byte[] container = new byte[1024 * 60]; DatagramPacket dp = new DatagramPacket(container, 0, container.length); //3、阻塞式接受包裹receive ds.receive(dp); //4、分析数据 byte[] datas = dp.getData(); int len = dp.getLength(); System.out.println(new String(datas, 0, len)); //5、释放资源 ds.close(); System.out.println("接收端关闭。。。"); } }TCP
package net.tcp; import java.io.DataInputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { // 1、指定端口创建服务端,使用ServerSocket创建服务端 ServerSocket server = new ServerSocket(8989); // 2、阻塞式等待连接accept Socket client = server.accept(); System.out.println("建立一个连接"); // 3、进行 *** 作:输入输出流 *** 作 DataInputStream dis = new DataInputStream(client.getInputStream()); String str = dis.readUTF(); System.out.println(str); // 4、释放资源 dis.close(); client.close(); server.close(); } }
package net.tcp; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; public class Client { public static void main(String[] args) throws IOException { // 1、建立连接,使用Socket创建客服端,传入服务端地址和端口 Socket client = new Socket("localhost", 8989); // 2、进行 *** 作,输入输出流 *** 作 DataOutputStream dos = new DataOutputStream(client.getOutputStream()); String str = "hello"; dos.writeUTF(str); // 3、释放资源 dos.close(); client.close(); } }网络编程应用-->爬虫基本原理
package net; import javax.net.ssl.HttpsURLConnection; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; public class SpiderTest { public static void main(String[] args) throws IOException { //获取URL URL url = new URL("https://www.baidu.com"); // InputStream is = url.openStream(); // BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.40"); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String temp = null; while (null != (temp = br.readLine())) { System.out.println(temp); } //获取资源后还需要分析和处理,再次不再深入 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)