0

I'm trying to download first bytes of a webpage.
I add Range to the HTTP request header. it's my code in C#:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com");

            request.AddRange(0,1000);
            //request.Proxy = null;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Stream st = response.GetResponseStream();
                StreamReader sr = new StreamReader(st);
                string str = sr.ReadToEnd();
                sr.Close();
                st.Close();
            }  

for some webpages it works fine, but some servers will ignore the HTTP range header, so the server send all the page in the response.
I changed my code to this:

string str = sr.Read(buffer,0,999);  

but it doesn't work! because the problem is not in this line. actually the response will send to my program when I call request.GetResponse()! in this line all the bytes received by the program and write the all bytes to the RAM. I want to stop receiving data from the server when I received first 1000 bytes.
but there is no control on HttpWebRequest class and .GetResponse() method to stop receiving data after 1000 bytes received.
How can I do that?
I think there would be another HTTP Request custom class that allow us to stop receiving data when we want.
please tell me any Idea about this problem. I'm thinking to override the HttpWebRequest or write a MFC Library (C++ language) and import it into my project, but I don't know how to do this.

EDIT: I know it's optional for server to allow or ignore the Range header! but my question is how can I stop receiving data from the server! for example the server is sending 10,000 bytes to my computer, I want to stop receiving bytes after I see the 1000th byte! (I don't want the server to send just first 1000 bytes, I want to close connection and stop receiving the bytes after first 1000 bytes! even if the server send all 10,000 bytes)

link pey
  • 39
  • 3
  • Directly use `response.GetResponseStream` to read bytes instead of *StreamReader* (which may also do internal caching). This doesn't mean server will not try to send the remaining bytes but at least you can close the connection in the middle. – Eser Aug 26 '15 at 17:11
  • @Eser `response.GetResponseStream` will return a pointer to first byte of data into the RAM! before it, when I call `.GetResponse()` it will download all the bytes from server to the RAM! so if I want to close the connection between my computer and server, i need to do some thing in the `.GetResponse()`. after this function every thins are in the computer RAM! – link pey Aug 26 '15 at 17:49
  • I hope it is not only what you think and you can back it up with some references(Suppose the link is an ISO of size 8GB. HttpWebRequest would never work on my old machine) – Eser Aug 26 '15 at 17:59
  • I'm not sure about what will happened when you start to get a hug i – link pey Aug 26 '15 at 18:53
  • I'm not sure about what will happened when you start to get a hug file, I think some of the file will load on RAM. but in my case I'm sure, because I run WireShark on my computer and capture incoming bytes when the program is running .GetResponse();. (there was a break point on response.GetResponseStream()) I see all data comes to my computer! you can test it too. I am going to test it on a hug file. – link pey Aug 26 '15 at 19:02
  • Suppose you want to read the first 1000 bytes from a link and you don't send a *range* request. How much would it take? Link is [this](http://217.20.145.43/?sig=77b5a1a61cd22129c3b1af59ad757bb001ae099e&ct=0&urls=217.20.157.202;217.20.153.79&expires=1440710200805&clientType=0&id=55801743917&type=3) which is about 600MB and I can read those bytes in a few milliseconds (using the codes in question). Question is: Do I have a very super ultra fast NW connection, or HttpWebRequest doesn't download it all before returning from *GetResponseStream* – Eser Aug 26 '15 at 21:36

0 Answers0