473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Hi,

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];
  2.  
  3.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query);
  4.                 HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
  5.                 Stream reader = resp.GetResponseStream(); 
  6.  
  7.                 int bytes_read = 0;
  8.                 int count;
  9.                 int bytes_to_read = 6000;
  10.  
  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.                 }
  18.  
  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
2 2660
Plater
7,872 Expert 4TB
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
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 = @"http://siteexplorer.search.yahoo.com/search?p=www.msn.com&bwm=i&bwms=p&bwmf=u&fr=sfp&fr 2=seo-rd-se"
Jan 19 '09 #3

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

Similar topics

1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
3
by: MAF | last post by:
I am trying to save an XMLDocument and I occasionally get a sharing violation. Can I force the write in this case? System.Xml.XmlDocument XMLDoc = new System.Xml.XmlDocument(); //Load file ...
4
by: Daniel P. | last post by:
I'm using StreamReader to read a text file and sometimes I get an error saying that the file is opened by someone else. I haven't found any info about how to set a flag to tell StreamReader to...
6
by: Jimbo | last post by:
After I read or write a file with streamreader and streamwriter and I close it with the Close method, does that automatically let go of the file so that any other process can modify it? In my...
6
by: No_Excuses | last post by:
All, I am interested in reading the text of a web page and parsing it. After searching on this newgroup I decided to use the following: ******************************* START OF CODE...
11
by: Tiger | last post by:
We can use seek() in the FileStream class,as we know. But I found that seek() is not work correctly in StreamReader. Who can tell me how to use seek() correctly in StreamReader? thanks a lot! I...
4
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
7
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
5
by: devgrt | last post by:
<--> We post some xml to https as shown below. The server supports SSL 3.0 and TLS. We wnat to force the PocketPC 2003 application to only use TLS for FIPs compliance. Is that possible? I see in...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.