简体   繁体   中英

HTTP GET request problem

I am writing a simple downloader. I am trying to download jpg picture.

void accel::download(int threads){
    char msg[] = "HEAD /logos/2011/cezanne11-hp.jpg HTTP/1.0\r\nConnection: close\r\n\r\n";
    int back = send(socketC, (const char *)&msg, strlen(msg), 0);
    char *buff = new char[500];
    back = recv(socketC, buff, 500, 0);

    cout << buff;

    char *buff2 = new char[700];
    char msg2[] = "GET /logos/2011/cezanne11-hp.jpg HTTP/1.0\r\nRange: bytes=0-400\r\nConnection: close\r\n\r\n";
    back = send(socketC, (const char *)&msg2, strlen(msg2), 0);
    back = recv(socketC, buff2, 700, 0);

    cout << back;

}

TCP connection is already initialized and the first part of my code is working. It succesfuly sends HEAD message and receaves response. But when it tries to download the picture, the recv gets 0. What might be wrong? Btw this is school project so I am not allowed to use some fancy libraries to perform this operation. This is full pictures address - http://www.google.com/logos/2011/cezanne11-hp.jpg

You don't recieve anything because you told the server that you didn't want to make a second request when you specified

Connection: close

In your HEAD request.

This tells the server that you're only going to make ONE request and not to bother waiting for a second.

Try changing your first request to a persistant 'keep-alive' connection.

"HEAD /logos/2011/cezanne11-hp.jpg HTTP/1.0\\r\\nConnection: keep-alive\\r\\n\\r\\n";

NOTE: If you don't want to server to go away you might want to change your second request to keep-alive too.

Generally speaking, HTTP closes the socket. (HTTP 1.1 has persistent (keep-alive) connections, though you seem to have asked the server to close the connection on you in your first command.)

So make sure that your socket is still open after your first receive; I'm willing to bet that it isn't.

Have you verified that the image is actually being sent to your client? Maybe you're not getting any response from the server.

Try using wireshark to inspect the actual network activity. That will let you see exactly what's being sent and received. It's possible you're not getting anything back from the server, or that there's an issue with your request that you might be able to spot in the actual network traffic.

After you follow through with what chrisaycock says you may want to add Host: to your requests. A lot of shared hosting around and IP only access is likely to start failing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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