pull text from a web site

  • Thread starter Thread starter Mark
  • Start date Start date
M

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
 
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();

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();
}
 
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
 
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 said:
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
 
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 said:
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,

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
 
Back
Top