gpt4 book ai didi

java - 使用自定义 Web 代理时 HTTP 响应未接收到所有数据

转载 作者:行者123 更新时间:2023-12-02 03:52:54 25 4
gpt4 key购买 nike

因此,我正在创建一个 Web 代理来捕获 HTTP 请求,确保 GET/POST/HEAD 是大写的,并修复第一行中的路径:

-------------------
------REQUEST------
-------------------
GET http://boundlessecho.com/story.html HTTP/1.1
Host: boundlessecho.com
Proxy-Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8


Port 80 Connected
-------------------
---FIXED REQUEST---
-------------------
GET /story.html HTTP/1.1
Host: boundlessecho.com
Proxy-Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

然后,它将这些数据发送到 DNS 请求后指定的服务器,该请求获取主机名和端口号(100 次中有 99 次,未指定端口,因此它会转到端口 80,这是默认的 HTTP 端口)。

然后,我将 HTTP 响应从服务器发送回客户端/浏览器;但是,很多时候,它会在接收 HTTP 响应部分时陷入困境。

BufferedReader feedback = new BufferedReader(new InputStreamReader(requestedServer.getInputStream()));
BufferedWriter showData = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"));

String line;
String header = "";

while (!(line = feedback.readLine()).equals("")) {
header += line + "\n";
}

int length = getLengthOfData(header);

showData.write(header, 0, header.length());
showData.write("\n", 0, 1);
showData.flush();

DataInputStream getBodyData = new DataInputStream(requestedServer.getInputStream());
DataOutputStream sendBodyData = new DataOutputStream(clientSocket.getOutputStream());

if (length != -1) {
byte[] b = new byte[length];
getBodyData.read(b, 0, length);
sendBodyData.write(b, 0, length);
sendBodyData.flush();
}

sendData.close();
showData.close();
feedback.close();
sendBodyData.close();
getBodyData.close();

查找内容长度的方法:

// Simply returns the Content-Length as an integer
public int getLengthOfData(String header) {

if (header.indexOf("Content-Length:") == -1) {
return -1;
}

return Integer.parseInt(header.substring(header.indexOf("Content-Length:") + 16, header.indexOf("\n", header.indexOf("Content-Length:"))));

}

有谁知道为什么它会卡在 Feedback.readLine() 上,或者我的代码中其他地方是否存在导致此问题的问题?

注意:它只会卡在某些请求上。有些请求有效!

注释 2:从原始帖子更新代码。

注3:代码再次更新。

最佳答案

您对回复的阅读不完整。您需要首先读取响应 header (读取直到到达表示 header 末尾的 CRLF CRLF 对),然后您需要分析 header 以了解是否以及如何进一步阅读数据。 HEAD 响应以及所有 1xx204304 响应没有正文内容,因此不要尝试为他们阅读 body 。其他任何内容都有主体,其长度取决于 Transfer-EncodingContent-LengthContext-Type 的特定值响应 header ,因此您需要查看它们并采取相应的操作。

阅读RFC 2616 Section 4.4 "Message Length"了解更多详细信息以及您必须遵循的确切规则。

本质上,您必须实现以下逻辑:

Read and parse HTTP headers
if not successful:
throw error
if response can contain message body:
if HTTP version is 1.1+ and Transfer-encoding is not identity:
while true:
read line, extract delimited ASCII hexadecimal, the chunk size
if not successful:
throw error
if chunk size is 0:
break while loop
read chunk size number of bytes
read and parse trailing HTTP headers
else if Content-Length is specified:
read Content-Length number of bytes
else if Content-Type is "multipart/byteranges":
read and parse MIME-encoded chunks until terminating MIME boundary is reached
else:
read until connection is closed

关于java - 使用自定义 Web 代理时 HTTP 响应未接收到所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35730428/

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