3

The funny thing is: I may have accidentally found the solution for Is it possible to send HTTP request using plain socket connection and receive response without headers?. I hope I'm overlooking something.

Anyway, I'm connecting to a web server, send a HTTP request, receive the response and close the connection. The actual HTTP conversation (tcpdump on the server side) looks like this:

GET /blah.php?param1=test1t&param2=test2 HTTP/1.0

HTTP/1.1 200 OK
Date: Sat, 22 Sep 2012 10:16:46 GMT
Server: Apache/2.2.17 (Ubuntu)
X-Powered-By: PHP/5.3.5-1ubuntu7.8
Vary: Accept-Encoding
Content-Length: 17
Connection: close
Content-Type: text/html

<pre>173364</pre>

Cool. It works... so far. Now this is the code. The string szRetval contains only "<pre>173364</pre>".

Socket s = new Socket("1.2.3.4", 80);
//DataInputStream in = new DataInputStream(s.getInputStream());
BufferedReader bin = new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream out = new DataOutputStream(s.getOutputStream());

out.writeBytes(szRequest);
String szRetval = "";
String szLine = "";
while((szLine=bin.readLine())!=null) {
    szRetval += szLine;
}
s.close();
return szRetval;

As you can see from the code sample, I've already switched from DataInputStream to BufferedReader. It makes no difference, so I guess this has to do with the Socket class itself.

Why o why are the http response headers not returned by a (raw) socket??

Android 2.3.3, Network I/O in AsyncTask, not using proxy.

Community
  • 1
  • 1
  • I wonder why the server is responding with HTTP/1.1 when you requested with HTTP/1.0. – James M Sep 29 '12 at 00:09
  • 1
    You sure you're not looking at `szLine` instead of `szRetval`? – James M Sep 29 '12 at 10:39
  • 1
    @JamesMcLaughlin [RFC 2145](http://tools.ietf.org/html/rfc2145): _"An HTTP server SHOULD send a response version equal to the highest version for which the server is at least conditionally compliant, and whose major version is less than or equal to the one received in the request"_. This is because a proxy that forwards the request might support a lower minor version, while the client is perfectly capable of understanding 1.1-specific headers. The proxy merely passes through the headers it doesn't understand, but alters the version in the request-line. I think you're right with the `szLine`. – CodeCaster Oct 25 '12 at 09:39
  • I put your code inside a mini java application to study this behaviour but i can't reproduce it. My doubt is about the boundary between http header and payload. Since it contains `CRLF` i suspect that some kind of "rewind" is operated on the string. Would you mind to print the content of `szLine` after every `readLine()`? – Davide Berra Jan 11 '13 at 11:49

0 Answers0