I happen to be writing a very simple text browser, and I'm having a problem connecting to servers using HTTP1.1 GET messages... HTTP1.0 works fine, I just have a string like this:

const char *request = "GET / HTTP/1.0\n\n";

This works great, and I receive my html back from the host just fine. But when I change it to a HTTP1.1 request, I get nothing back...

//  Assuming that I'm connecting to google...
const char *request = "GET / HTTP/1.1\nHost: www.google.com\n"

Any ideas as to why I'm not getting anything back from the server?

-Fredric

Dani AI

Generated

Short version: the problem was the exact bytes sent, not the GET itself. was right to suggest the extra blank line and correctly pointed out the line-ending detail; those together are why started getting data back.

Why this happens: HTTP/1.1 parsers expect header lines terminated by CR+LF and an empty CRLF pair to mark the end of the header section. When you write to a TCP socket you send raw bytes — the system does not convert \n into \r\n for socket writes (Windows text-mode translation only affects stdio file streams, not sockets). Many HTTP/1.0 servers and tolerant proxies accept a lone LF, but HTTP/1.1 servers and front-end proxies can wait for the exact CRLF sequences and therefore appear to hang if the \r is missing. Also remember HTTP/1.1 requires a Host: header for name-based virtual hosting.

Concrete troubleshooting and hardening tips:

  • Inspect the bytes you send with tcpdump/Wireshark or a hex dump to confirm you are sending 0x0D 0x0A pairs and a final empty CRLF pair.
  • For a simple client, add Connection: close so the server will close the socket when the response is done (this avoids having to implement persistent-connection bookkeeping).
  • Be prepared to handle Transfer-Encoding: chunked or respect Content-Length so you know when the response body ends.
  • If a request still seems to hang, try shutting down the write side of the socket (or closing it) after sending the request to signal end-of-request.
    Following those checks will make a minimal HTTP/1.1 text browser robust and explain why the LF-only HTTP/1.0-style request worked while the HTTP/1.1 version did not.

Hmm, perhaps you need another newline all by itself?

Try

//  Assuming that I'm connecting to google...
const char *request = "GET / HTTP/1.1\nHost: www.google.com\n\n"

That doesn't seem to work, although it should. I have no idea what's going on, I think I'll just have to settle with HTTP1.0...

-Fredric

this should work:
const char *request = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"

http protocol uses \r\n as a new line, and the last line must always be empty.

I put the \r in there, and I started getting data from the server!

Thanks. :)

-Fredric

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.