473,394 Members | 1,813 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,394 software developers and data experts.

pull text from a web site

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
Nov 16 '05 #1
5 1825
"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in
news:ev**************@TK2MSFTNGP11.phx.gbl:
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();
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2
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" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in
news:ev**************@TK2MSFTNGP11.phx.gbl:
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();
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #3
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" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:eY**************@TK2MSFTNGP12.phx.gbl...
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" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in
news:ev**************@TK2MSFTNGP11.phx.gbl:
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();
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/


Nov 16 '05 #4
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" <so*****@nospam.com> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
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" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:eY**************@TK2MSFTNGP12.phx.gbl...
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" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message news:Xn**********************************@207.46.2 48.16...
"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in
news:ev**************@TK2MSFTNGP11.phx.gbl:

> 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();
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/



Nov 16 '05 #5
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" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:eZ*************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mike | last post by:
I need to be able to pull the current <Title> tag from the header into the body of the html page. We started coding this with php, and we were able to get it working. However the entire page is...
2
by: Michael | last post by:
I know how to enter data to a SQL database from a form. I am making a form where the user inputs some numbers. Than other users login to this other place and I want the number that the person...
0
by: Karthik | last post by:
Hi, We have setup a publisher and a distributor in SQL 2000 running SP3. Alongwith this we have setup a merge replication agent and its running successfully inside the same network. I allow...
5
by: Glenn Mulno | last post by:
Hi, I am trying to create a report page for several teams. Each team runs the same reports but the reports use different values. For a while this was getting insane everytime I updated the...
5
by: kuljitsingh | last post by:
I remember making a client/server application using Java and CORBA where the server was pushing any change data to all the clients. I have not ventured the same area in C#. Is there a way to...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
4
by: itcassy | last post by:
I would like to pull a random or alphabetical result on the home page of a site and leave that same result on there for one week. The closest example I have found online is Random Quote Version...
19
by: jim | last post by:
(from http://www.news.com/8301-13578_3-9798715-38.html ) October 16, 2007 5:56 PM PDT RIAA tries to pull plug on Usenet. Seriously. Posted by Declan McCullagh The Recording Industry Association...
3
by: Baron Samedi | last post by:
I am looking for a reliable cross-browser pull-quote solution in CSS. See for instance, http://www.sitepoint.com/examples/pullquotes/ The problem seems at first to be sure that it functions...
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: 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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.