gpt4 book ai didi

java - 无法从 Google Chrome 中的 Java Server 应用程序接收纯文本

转载 作者:行者123 更新时间:2023-12-02 11:28:27 25 4
gpt4 key购买 nike

我有一个用 Java 编写的小型服务器应用程序,它将纯文本发送到浏览器,如下所示:

服务器.java

import java.io.IOException;
public class Server {

private int portNumber;

public Server(int portNumber) {
this.portNumber = portNumber;
}

public void start() {
//Thread pool
final ExecutorService threadPool = Executors.newFixedThreadPool(10);
Runnable serverTask = new Runnable() {

@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(portNumber);
while(true) {
Socket clientSocket = ss.accept();
threadPool.submit(new ClientTask(clientSocket));
}
} catch(IOException e) {
e.printStackTrace();
}
}
};
Thread clientThread = new Thread(serverTask);
clientThread.start();
}
}

ClientTask.java

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ClientTask implements Runnable{

private final Socket clientSocket;

public ClientTask(Socket clientSocket) {
this.clientSocket = clientSocket;
}

@Override
public void run() {
System.out.println("Got a client...");

try (BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream os = new DataOutputStream(clientSocket.getOutputStream());){
os.writeBytes("HTTP/1.1 200 OK\r\n");
os.writeBytes("Content-Type: text/plain");
os.writeBytes("\r\n");
os.writeBytes("\r\n");
os.writeBytes("hello");
os.writeBytes("\r\n");
os.flush();
System.out.println("About to close..");
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}

}

Main.java

public class Main {

public static void main(String[] args) {
Server server = new Server(1111);
server.start();
}
}

我的服务器在端口 1111 上运行,当我在 Safari 中输入 http://localhost:1111 时,它运行得很好(我在浏览器中看到“hello”),但在 Chrome 中则不然。
Chrome 会出现什么情况?

编辑:控制台网络选项卡的屏幕截图 enter image description here

最佳答案

您在 DataOutputStream 中发送的输出是错误的,基本上 Chrome 会接受有效的 HTTP 响应来接收。

这是您的客户端更新(我会让您优化代码)

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ClientTask implements Runnable {

private final Socket clientSocket;

public ClientTask(Socket clientSocket) {
this.clientSocket = clientSocket;
}

@Override
public void run() {
System.out.println("Got a client...");

try (BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream os = new DataOutputStream(clientSocket.getOutputStream())) {
String message = "Hello";
os.writeBytes("HTTP/1.0 200 OK\r\n");
final String currentDateFormatted = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC));
os.writeBytes("Date: " + currentDateFormatted + "\r\n");
os.writeBytes("Server: Java Server/12234\r\n");
os.writeBytes("Content-Type: text/plain\r\n");
os.writeBytes("Content-Length: " + message.length() + "\r\n");
os.writeBytes("Expires: " + currentDateFormatted + "\r\n");
os.writeBytes("Last-modified: " + currentDateFormatted + "\r\n");
os.writeBytes("\r\n");
os.write(message.getBytes(StandardCharsets.UTF_8));
os.flush();
os.close();
System.out.println("About to close..");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

在 Chrome 上测试并通过终端使用 curl:

curl -v localhost:1111
* Rebuilt URL to: localhost:1111/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 1111 (#0)
> GET / HTTP/1.1
> Host: localhost:1111
> User-Agent: curl/7.54.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Sat, 24 Mar 2018 11:27:15 GMT
< Server: Java Server/12234
< Content-Type: text/plain
< Content-Length: 5
< Expires: Sat, 24 Mar 2018 11:27:15 GMT
< Last-modified: Sat, 24 Mar 2018 11:27:15 GMT
<
* Closing connection 0
Hello%

关于java - 无法从 Google Chrome 中的 Java Server 应用程序接收纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49462568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com