我正在尝试用 Java 编写一个 HTTP 服务器。在查看示例并添加一些调试内容之后,这就是我到目前为止所拥有的:
import java.net.*;
import java.io.*;
import java.util.*;
public class AddyServer {
public static void main(String[] args) {
int portNumber = Integer.parseInt(args[0]);
try{
System.err.println("Trying...");
ServerSocket serverSocket = new ServerSocket(portNumber);
System.err.println("Waiting...");
//PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
while(true) {
System.err.println("Waiting...");
Socket clientSocket = serverSocket.accept();
System.err.println("accepted");
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
String line;
while((line=in.readLine())!=null) {
if(line.isEmpty())
break;
out.print(line+"\r\n");
System.out.print(line+"\r\n");
}
out.print("HTTP/1.1 200\r\n"); //EDITED
out.print("Content-Type: text/html\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
out.print("<!doctype html>\n");
out.print("<title>Test title</title>\n");
out.print("<p>Test</p>\n");
System.out.print("HTTP/1.1 200 \r\n");
System.out.print("Content-Type: text/html\r\n");
System.out.print("Connection: close\r\n");
System.out.print("\r\n");
System.out.print("<!doctype html>\n");
System.out.print("<title>Test title</title>\n");
System.out.print("<p>Test</p>\n");
in.close();
out.close();
clientSocket.close();
}
} catch (IOException e) {System.err.println(e);} //EDITED
}
}
我不明白为什么当我尝试连接到我的服务器时,我的互联网浏览器没有在浏览器中显示 HTTP 响应正文。
最佳答案
写入套接字后添加刷新(通过 PrintWriter)。此外,您还需要在某个时候关闭 serverSocket,否则,仅在端口已绑定(bind)的情况下测试它就会遇到问题。
编辑这准确地显示了您要刷新的位置。经过测试并为我工作。
工作固定代码:
import java.net.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class AddyServer {
public static void main(String[] args) throws Exception {
int portNumber = Integer.parseInt(args[0]);
System.err.println("Trying...");
ServerSocket serverSocket = new ServerSocket(portNumber);
System.err.println("Waiting...");
// PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
// true);
while (true) {
System.err.println("Waiting...");
Socket clientSocket = serverSocket.accept();
System.err.println("accepted");
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
String line;
while ((line = in.readLine()) != null) {
if (line.isEmpty())
break;
out.print(line + "\r\n");
System.out.print(line + "\r\n");
}
out.print("HTTP/1.1 200 \r\n");
out.print("Content-Type: text/html\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
out.print("<!doctype html>\n");
out.print("<title>Test title</title>\n");
out.print("<p>Test</p>\n");
out.flush(); // <--- Fix is here
System.out.print("HTTP/1.1 200 \r\n");
System.out.print("Content-Type: text/html\r\n");
System.out.print("Connection: close\r\n");
System.out.print("\r\n");
System.out.print("<!doctype html>\n");
System.out.print("<title>Test title</title>\n");
System.out.print("<p>Test</p>\n");
in.close();
out.close();
clientSocket.close();
}
}
}
关于java - 为什么我的 HTTP 响应没有显示在浏览器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215848/