Java HTTP:修订间差异
跳到导航
跳到搜索
(创建页面,内容为“==== Server ==== import java.net.ServerSocket; import java.net.Socket; ServerSocket serverSocket = new ServerSocket(port0); Socket socket = serverSocket.accept(); # String IP_C = socket.getInetAddress().getHostAddress(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); inputLine = in.readLine(); out.println("String"); # Http 返回结构 String httpRespo…”) |
无编辑摘要 |
||
第1行: | 第1行: | ||
==== Server ==== | ==== Server ==== | ||
import java.net.ServerSocket; | <small><nowiki>import java.net.ServerSocket; | ||
import java.net.Socket; | import java.net.Socket; | ||
第19行: | 第19行: | ||
httpResponseMessage; | httpResponseMessage; | ||
out.print(httpResponse); | out.print(httpResponse); | ||
out.flush(); | out.flush();</nowiki></small> | ||
==== Client ==== | ==== Client ==== | ||
import java.net.Socket; | <small><nowiki>import java.net.Socket; | ||
Socket socket1 = new Socket(host1, port1); | Socket socket1 = new Socket(host1, port1); | ||
第31行: | 第31行: | ||
out.println("String"); | out.println("String"); | ||
response = in.readLine(); | response = in.readLine();</nowiki></small> | ||
[[分类:Develop]] | [[分类:Develop]] | ||
[[分类:Java]] | [[分类:Java]] |
2024年8月8日 (四) 17:01的最新版本
Server
import java.net.ServerSocket; import java.net.Socket; ServerSocket serverSocket = new ServerSocket(port0); Socket socket = serverSocket.accept(); # String IP_C = socket.getInetAddress().getHostAddress(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); inputLine = in.readLine(); out.println("String"); # Http 返回结构 String httpResponse = "HTTP/1.1 200 OK\r\n" + String.format("Content-Length: %s\r\n", httpResponseMessage.length()) + "Content-Type: text/plain\r\n" + "\r\n" + httpResponseMessage; out.print(httpResponse); out.flush();
Client
import java.net.Socket; Socket socket1 = new Socket(host1, port1); BufferedReader in1 = new BufferedReader(new InputStreamReader(socket1.getInputStream())); PrintWriter out1 = new PrintWriter(socket1.getOutputStream(), true); socket1.setSoTimeout(timeout); out.println("String"); response = in.readLine();