I am using the object Socket to do basic connection to download a file but I need to know the size of the file ahead of time. So is there any socket function of HTTP function that can find the file size?
I am using the object Socket to do basic connection to download a file but I need to know the size of the file ahead of time. So is there any socket function OR HTTP function that can find the file size? PS: I posted again to correct the or. Sorry for the typo.
Well, you didn't quite answer my question, but I'll assume that you're using plain Sockets to connect to a Web server to download the file. If you use HTTP 1.0 commands to fetch the file, you'll get a "Content-Length:" header which you can parse to get the length of the file -- i.e., like the below. The line "GET / HTTP/1.0" is the key thing you have to send; the Content-Length header will come back and you can parse it. It's a lot easier to use URLConnection, though! $ telnet localhost 80 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.0 HTTP/1.1 200 OK Date: Wed, 25 Feb 2004 22:13:40 GMT Server: Apache/2.0.48 (Unix) DAV/2 SVN/1.0.0 Last-Modified: Mon, 23 Feb 2004 21:22:09 GMT ETag: "50421c-ae5-a0490240" Accept-Ranges: bytes Content-Length: 2789 Connection: close Content-Type: text/html; charset=ISO-8859-1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head>
Thank you for your respone.Your assumption is correct. However, the solution you suggested didn't work well I used: telnet yahoo.com 80 The response was: Trying 22.214.171.124... Connected to yahoo.com. Escape character is '^]'. I then typed: GET / HTTP/1.0 No response: If I used instead GET /HTTP/1.0 I get the index.html file by the GET command. So what is the problem?
Look back carefull at the transcript I showed you. When you use the HTTP/1.0 form, you have to enter a blank line before you'll get a response. That's because HTTP/1.0 requests can themselves include headers; the extra blank line indicates the end of the request. I'm guessing that asking Yahoo for a non-existent page just returns their index page; smooshing the "/" and "HTTP/1.0" together makes it look like an HTTP 0.9 reqeust for a non-existent page.