3

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:

(1) closing the connection;
(2) examining Content-Length;
(3) getting all chunks in the case of 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.

I want a general-case solution and do not want to
add a Connection: close line to the request itself.

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?

UPD: forgot to mention that the HTTP request is given to me as a sequence of bytes. Yes, it is for testing.

PS
relevant reading:
End of an HTTP Response
Detect the end of an HTTP Request in Java
Detect end of HTTP request body
How HTTP Server inform its clients that the response has ended
Proper handling of chuncked Http Response within Socket
Detect the end of a HTTP packet
Android socket & HTTP response headers
Java HTTP GET response waits until timeout

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • "*... way to detect the end of http response*": The answers should all be in here: http://tools.ietf.org/html/rfc7230 (chapters 3.2 and 3.3) – alk Sep 04 '14 at 10:17
  • @alk are you crazy? who would read RFCs ;-) – pskink Sep 04 '14 at 10:25
  • Such nitpickers like me ...? ;-) @pskink Also I feel it's worth mentioning that the http related RFCs had been rewritten. Clarifying lots of ambiguities .. – alk Sep 04 '14 at 10:26
  • The first thing that comes to my mind is to use HttpURLConnection, in which the problem is already solved. You just read the input stream until end of stream. It takes care of when that should happen. – user207421 Sep 04 '14 at 10:28
  • @alk you are the exception, i bet that 50% of folks here even don't know that such a thing like RFC even exists.... – pskink Sep 04 '14 at 10:31
  • @alk I do not have enough resources to debug and then test code written from scratch up to RFC. In addition, in real life there always are gotchas (e.g. servers reject queries if the user-agent is not "Mozilla"), and I do not have time to learn them all the hard way. – 18446744073709551615 Sep 04 '14 at 11:01

1 Answers1

0

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.

The Apache Http Client will give you access to the headers and their content, which may be enough for you.

If you really need access to the actual character sequence sent by the server (e.g. 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

jsalvata
  • 2,155
  • 15
  • 32