gpt4 book ai didi

java - 读取请求使服务器停止

转载 作者:行者123 更新时间:2023-12-01 04:59:51 25 4
gpt4 key购买 nike

我在 Java 套接字方面还有另一个问题。服务器的套接字没有按照应有的方式解释请求。我认为读取 HTML header 之后的行有问题,但我不知道这段代码有什么问题。 TIA。

这是代码片段:

@Override
public void run() {
DataOutputStream dout = null;
BufferedReader reader = null;
try {
dout = new DataOutputStream(socket.getOutputStream());
reader = new BufferedReader(
new InputStreamReader(
socket.getInputStream(), UTF-8"));

String requestString = reader.readLine();
StringTokenizer tokenizer = new StringTokenizer(requestString);
String httpMethod = tokenizer.nextToken();
String httpQueryString = tokenizer.nextToken();

System.out.println("method: " + httpMethod);
System.out.println("query: " + httpQueryString);
String line;
int i = 0;
while ( ! (line = reader.readLine())
.equals("")) {
System.out.println(i++ + " : " + line);
}

//DEBUG
System.out.println("foo");
// HERE IS THE PROBLEM !!!
line = reader.readLine();
System.out.println("aaa " + line);
line = reader.readLine();
System.out.println("bbb " + line);
line = reader.readLine();
System.out.println("ccc " + line);

// Pseudocode
if (GET) {
if ("/") {
...
} else if (isFile) {
...
} else {
...
}
} else if (POST) {
... //TODO
} else {
Error 404
}

} catch (Exception e) {
e.printStackTrace();
} finally {
//Cleaning
try {
reader.close();
dout.close();
socket.close();
} catch (IOException e) {
Logger.getAnonymousLogger().warning("Socket cannot be closed");
}
}
}

我得到的输出:

INFO: Server is RUNNING
INFO: Connection accepted
method: GET
query: /
0 : Host: 127.0.0.1:8001
1 : User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0
2 : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
3 : Accept-Language: pl,en-us;q=0.7,en;q=0.3
4 : Accept-Encoding: gzip, deflate
5 : DNT: 1
6 : Connection: keep-alive
7 : Cache-Control: max-age=0
foo

--- in this place server halts ---
--- then I refresh page or do anything else that sends request (GET, POST) ---
--- and server receives 'remaining' part of the request ---

aaa null // in POST this line has send values
bbb null
ccc null
INFO: method = GET
INFO: Connection accepted
method: GET
query: /
0 : Host: 127.0.0.1:8001
1 : User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0
2 : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
3 : Accept-Language: pl,en-us;q=0.7,en;q=0.3
4 : Accept-Encoding: gzip, deflate
5 : DNT: 1
6 : Connection: keep-alive
foo

--- in this place server halts ---
--- then I refresh page or do anything else that sends request (GET, POST) ---
--- and server receives 'remaining' part of the request ---

最佳答案

请求没有剩余部分。 GET 请求在空行之后停止。服务器被阻止,因为没有传入数据(直到下一个请求)

关于java - 读取请求使服务器停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13536347/

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