By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

How to force StreamReader.Close() not to Read?

P: 4

I am trying to read first 6000 bytes from a webpage, using StreamReader.Read(buffer, offset, no_of_bytes_to_read) method and trying to close the connection. Because, the useful data I need is present in the first 6000 bytes of the webpage and webpage size is atleast 100Kb. And, I need to repeat this operation for around 100s of such pages. Thats why I would close the connection after I am done reading initial 6000 bytes.

Now the problem is, I noticed that when code fires streamRead.Close(), it actually reads those remaining 94Kb of Data and then closes the connection !! This consumes extra 8 to 10 seconds just for closing the reader.(Verified via network transfer logs and the debug-time timestamps before and after this streamRead.Close() statement.)

Isn't this strange that why the reader should read the remaining bytes when I am actually telling it to close the connection and quit. Maybe I am doing it incorrectly, I tried different methods but none working. Is there other to achieve what I just need?

Expand|Select|Wrap|Line Numbers
  1.                 byte[] bytes = new byte[6000];
  3.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query);
  4.                 HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
  5.                 Stream reader = resp.GetResponseStream(); 
  7.                 int bytes_read = 0;
  8.                 int count;
  9.                 int bytes_to_read = 6000;
  11.                 while (bytes_read < 6000)
  12.                 {
  13.                     count = reader.Read(bytes, bytes_read, bytes_to_read);
  14.                     MessageBox.Show(System.Text.ASCIIEncoding.ASCII.GetString(bytes));
  15.                     bytes_read += count;
  16.                     bytes_to_read -= count;
  17.                 }
  19.                 reader.Close(); /// <-- This Line takes 8 to 10 seconds to finish its work because it reads remaining page from server and then closes
  20.                 resp.Close();
  21.                 string response = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
- Vishal
Jan 19 '09 #1
Share this Question
Share on Google+
2 Replies

Expert 5K+
P: 7,872
I am guessing it has something to do with the Flush properties of the stream.
There is probably a low level stream control flag that you can set to tell it not to flush its buffer on close.
You could also maybe just try doing a .Dispose()?
Jan 19 '09 #2

P: 4
Yes, I thought on same line about the flag thing to change this behavior but didn't find one so far.

I tried Dispose() already, it doesn't solve the issue.

Also note that, this happens with this particular web-server. Same code works fine on other web-servers.

Here is the value of query which causes this issue.

String query = @" 2=seo-rd-se"
Jan 19 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.