Search:
Namespaces
Discussions
.NET v1.1
Feedback
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
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com