网络编程
Posted hunter-w
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络编程相关的知识,希望对你有一定的参考价值。
URL对象
-
URL类
-
URL(Uniform Resource Locator)
一致资源定位器的简称,表示Internet上某一资源的地址
-
URL的组成
protocol:resourceNamae
协议名指明获取资源所使用的的传输协议,如http、ftp等,资源名则是资源的完整地址,包括主机名、端口号或文件内部的一个引用。
-
获取URL对象属性
Public String getProtocol() Public String getHost() public String getPort() public String getFile()
-
URLConnection对象
-
一个URLConnection对象代表一个URL资源与Java程序的通讯连接,可以通过它对这个URL资源读或写。
-
与URL的区别
-
URL是单向的,URLConnection是双向的
-
可以查看服务器的响应消息的首部
-
可以设置客户端请求消息的首部
-
-
使用示例
关键步骤:构造一个URL对象,调用URL对象的
openConnection()
方法获取对应该URL的URLConnection对象。public class URLConnector { public static void main(String[] args) { try { URL cs = new URL("http://www.sina.com/"); URLConnection tc = cs.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream())); String inputLine; while((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Get与Post请求
-
示例
public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" +param; URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.connect(); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while((line=in.readLine()) != null) { result += line; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } } public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setDoOutput(true); out = new PrintWriter(connection.getOutputStream()); out.print(param); out.flush(); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while((line=in.readLine()) != null) { result += line; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } }
-
HttpURLConnection类
在URLConnection的基础上提供一系列针对http请求的内容
- HTTP状态码(例如HTTP_OK:200)
setRequestMethod
(设置请求方法GETPOST等)getResponseCode
(获取HTTP的响应)
Socket通信原理
-
TCP传输协议(Transport Control Protocol)
面向连接的能够提供可靠的流式数据传输的协议
URL,URLConnection,Sokcet,ServerSocket等类都使用TCP协议进行网络通讯。
-
socket通讯
网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个socket。
Socket通信实现
-
创建socket
使用Socket类构造
-
客户端socket的建立
-
服务端socket的建立
-
打开输入输出流
-
关闭socket
注意先关闭流再关闭socket
@TalkClient.java
import java.net.*; import java.io.*; public class TalkClient { public static void main(String[] args) { System.out.println("Client Start..."); try { Socket socket = new Socket("127.0.0.1", 8888); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); String readline; readline = sin.readLine(); while (!readline.equals("bye")) { os.println(readline); os.flush(); System.out.println("Client:" + readline); System.out.println("Server:" + is.readLine()); readline = sin.readLine(); } os.close(); is.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
@TalkServer.java
public class TalkServer { public static void main (String[] args) { System.out.println("Server start..."); try { ServerSocket server = new ServerSocket(8888); Socket socket = server.accept(); String readline; BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Client: "+is.readLine()); readline = sin.readLine(); while (!readline.equals("bye")) { os.println(readline); os.flush(); System.out.println("Server:" + readline); System.out.println("Client:" + is.readLine()); readline = sin.readLine(); } os.close(); is.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); } } }
注意os.print()与os.println()的区别,不要被回车符坑了!!!
以上是关于网络编程的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装
VSCode自定义代码片段14——Vue的axios网络请求封装