.NETGURU
pull text from a web site
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.

Post a new message to this list...

Mark
Using the code below, I can pull the text from a file and store it in a
string. I'd like to be able to do the same thing with a web site ...
perhaps www.cnn.com. How can you do this?

StreamReader objStreamReader = File.OpenText("c:\somefile\blah.txt");
//Replacing this with a URL errors out.
string strMyString = objStreamReader.ReadToEnd();
objStreamReader.Close();

Thanks in advance.
Mark

Reply to this message...
 
    
Chris R. Timmons
"Mark" <Click here to reveal e-mail address> wrote in
news:Click here to reveal e-mail address:

[Original message clipped]

Mark,

public static string GetUrlAsString(string url)
{
WebRequest wReq = WebRequest.Create(new Uri(url));
using (WebResponse wResp = wReq.GetResponse())
using (Stream respStream = wResp.GetResponseStream())
using (StreamReader reader =
new StreamReader(respStream, Encoding.ASCII))
return reader.ReadToEnd().Trim();
}

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Reply to this message...
 
    
Mark
Worked beautifully. However, I imagine you didn't mean to include that many
Using statements?? The modified code below worked perfectly...

WebRequest wReq = WebRequest.Create(new Uri(url));
WebResponse wResp = wReq.GetResponse();
Stream respStream = wResp.GetResponseStream();
StreamReader reader = new StreamReader(respStream, Encoding.ASCII);
return reader.ReadToEnd().Trim();

Thanks!
Mark

"Chris R. Timmons" <Click here to reveal e-mail address> wrote in message
news:Xns955F88F516609crtimmonscrtimmonsin@207.46.248.16...
[Original message clipped]

Reply to this message...
 
    
Marina
The idea behind the using statements, was to make sure to close all the
streams to release all resources when the job was finished. In your example,
they all remain open.

"Mark" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> Worked beautifully. However, I imagine you didn't mean to include that
many
[Original message clipped]

Reply to this message...
 
    
Mark
True, but doesn't the use of THREE using statements mess up the scope of
some of the variables? For example, wResp is out of scope by the time the
second Using statement executes - no? The code as-is didn't compile for me.

Thanks.

Mark

"Marina" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
oj
Mark,

It should work fine as-is. You would need to import a few needed namespaces.

using System.Text; //Encoding
using System.IO; //Stream/Reader
using System.Net; //WebResponse/Request

If you don't want those namespaces imported, you could do this:

public static string GetUrlAsString(string url)
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(new Uri(url));
using (System.Net.WebResponse wResp = wReq.GetResponse())
using (System.IO.Stream respStream = wResp.GetResponseStream())
using (System.IO.StreamReader reader =
new System.IO.StreamReader(respStream, System.Text.Encoding.ASCII))
return reader.ReadToEnd().Trim();
} //end GetUrlAsString

"Mark" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.IO.File
System.IO.Stream
System.IO.StreamReader
System.Net.WebRequest
System.Net.WebResponse
System.Text.Encoding
System.Uri




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification