简体   繁体   English

一种检测http响应结束的简单方法(原始套接字,java)?

[英]An easy way to detect the end of http response (raw socket, java)?

I want to retrieve the server's response as is, with all headers. 我想使用所有标头按原样检索服务器的响应。 The first thing that comes to mind is to use raw sockets. 首先想到的是使用原始套接字。 As I have learned from the search, there are 3 ways to indicate the end of response: 从搜索中了解到,有3种方法指示响应结束:

(1) closing the connection; (1)关闭连接;
(2) examining Content-Length ; (2)检查Content-Length
(3) getting all chunks in the case of Transfer-Encoding: Chunked . (3)在Transfer-Encoding: Chunked的情况下获取所有块。
There is also 也有
(4) the timeout method: assume that the timeout means end of data, but the latter is not really reliable. (4)超时方法:假定超时意味着数据结束,但是后者并不真正可靠。

I want a general-case solution and do not want to 我想要一个一般情况的解决方案,并且不想
add a Connection: close line to the request itself. 向请求本身添加一个Connection: close行。

In addition, it is recommended to use an existing library rather than re-invent the wheel. 另外,建议使用现有的库而不是重新发明轮子。

Question: 题:

How do I use an existing package, preferably, something already present in Android, to detect the end of HTTP response while having access (without interference) to the raw data stream? 如何使用现有程序包(最好是Android中已存在的程序包)来检测HTTP响应的结束,同时可以访问(无干扰)原始数据流?

UPD: forgot to mention that the HTTP request is given to me as a sequence of bytes. UPD:忘了提及HTTP请求是按字节顺序发送给我的。 Yes, it is for testing. 是的,它用于测试。

PS 聚苯乙烯
relevant reading: 相关阅读:
End of an HTTP Response HTTP响应结束
Detect the end of an HTTP Request in Java 检测Java中HTTP请求的结束
Detect end of HTTP request body 检测HTTP请求主体的结尾
How HTTP Server inform its clients that the response has ended HTTP Server如何通知其客户端响应已结束
Proper handling of chuncked Http Response within Socket 在套接字中正确处理分块的HTTP响应
Detect the end of a HTTP packet 检测HTTP数据包的结尾
Android socket & HTTP response headers Android套接字和HTTP响应标头
Java HTTP GET response waits until timeout Java HTTP GET响应等待直到超时

I suggest to use a the Apache HTTP client package ( http://hc.apache.org/httpclient-3.x/ ) so you don't need to implement all the finicky details of the HTTP protocol. 我建议使用Apache HTTP客户端程序包( http://hc.apache.org/httpclient-3.x/ ),因此您无需实现HTTP协议的所有细节。

The Apache Http Client will give you access to the headers and their content, which may be enough for you. Apache Http Client将使您能够访问标头及其内容,这对您而言可能就足够了。

If you really need access to the actual character sequence sent by the server (eg for debugging purposes), you could then intercept the communication by replacing the connection socket factory with your own to create "intercepting" sockets which store all data transferred in a buffer where your code can access it later on. 如果您确实需要访问服务器发送的实际字符序列(例如出于调试目的),则可以通过用自己的连接套接字工厂替换来创建“拦截”套接字来拦截通信,该套接字将所有传输的数据存储在缓冲区中您的代码以后可以访问它的位置。 See http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/connmgmt.html#d5e418 参见http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/connmgmt.html#d5e418

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM