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

how to get a web page



HI all...

I'm doing the following to get a web page:

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.google.com");
HttpWebResponse wresp = (HttpWebResponse)wr.GetResponse();
Stream wpage = wr.GetRequestStream();
byte[] buffer = new byte[5000];
wpage.Read(buffer,0,5000);

Now, how can I transform the buffer in a string so I can do a
regex.ismatch on the string???
thx...

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
8 1402
Ricardo,

You will need to run the bytes through an encoder. Most likely this
will be an AsciiEncoder, but you can check the ContentEncoding property of
the HttpWebResponse to get the specific encoding.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Luceac" <rl*****@gmail.com> wrote in message
news:uf**************@TK2MSFTNGP12.phx.gbl...


HI all...

I'm doing the following to get a web page:

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.google.com");
HttpWebResponse wresp = (HttpWebResponse)wr.GetResponse();
Stream wpage = wr.GetRequestStream();
byte[] buffer = new byte[5000];
wpage.Read(buffer,0,5000);

Now, how can I transform the buffer in a string so I can do a
regex.ismatch on the string???
thx...

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2


Now I'm getting an error that when I do the:

wpage.Read(buffer,0,5000);

There's an error saying that the stream flow is not open.

What's that???

thx...

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Ricardo,

Are you trying to read the contents twice? Or perhaps you have disposed
of the response already?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Luceac" <rl*****@gmail.com> wrote in message
news:OP**************@TK2MSFTNGP15.phx.gbl...


Now I'm getting an error that when I do the:

wpage.Read(buffer,0,5000);

There's an error saying that the stream flow is not open.

What's that???

thx...

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4


No... I only do it one time... Here's my code..

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.google.com");
wProxy.Credentials=cr;
wr.Proxy=wProxy;
HttpWebResponse wresp;
try
{
wresp = (HttpWebResponse)wr.GetResponse();
if(wresp.StatusCode==HttpStatusCode.OK)
{
blnConnected=true;
}
}
catch(System.Net.WebException)
{
blnConnected = false;
}

if(!blnConnected)

lvcItem.SubItems[1].Text="Not Connected";
else
{
Stream wpage = wr.GetRequestStream();
byte[] buffer = new byte[5000];
wpage.Read(buffer,0,5000);
string swpage = ASCIIEncoding.ASCII.GetString(buffer);
Regex rg = new Regex("Google");
bool isProxy = rg.IsMatch(swpage);
if(isProxy)
lvcItem.SubItems[1].Text="OK - It's a Proxy";
else
lvcItem.SubItems[1].Text="Sorry, not a Proxy";
I don't know what's happening... If you could help me...

thx...

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
Ricardo,

You are using GetRequestStream from the request. You want to issue the
request to get the response (through the GetResponse method on WebRequest),
and then get the response stream and read that (through the
GetResponseStream).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Luceac" <rl*****@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...


No... I only do it one time... Here's my code..

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.google.com");
wProxy.Credentials=cr;
wr.Proxy=wProxy;
HttpWebResponse wresp;
try
{
wresp = (HttpWebResponse)wr.GetResponse();
if(wresp.StatusCode==HttpStatusCode.OK)
{
blnConnected=true;
}
}
catch(System.Net.WebException)
{
blnConnected = false;
}

if(!blnConnected)

lvcItem.SubItems[1].Text="Not Connected";
else
{
Stream wpage = wr.GetRequestStream();
byte[] buffer = new byte[5000];
wpage.Read(buffer,0,5000);
string swpage = ASCIIEncoding.ASCII.GetString(buffer);
Regex rg = new Regex("Google");
bool isProxy = rg.IsMatch(swpage);
if(isProxy)
lvcItem.SubItems[1].Text="OK - It's a Proxy";
else
lvcItem.SubItems[1].Text="Sorry, not a Proxy";
I don't know what's happening... If you could help me...

thx...

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #6
Ricardo Luceac wrote:


HI all...

I'm doing the following to get a web page:

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.google.com");
HttpWebResponse wresp = (HttpWebResponse)wr.GetResponse();
Stream wpage = wr.GetRequestStream();
byte[] buffer = new byte[5000];
wpage.Read(buffer,0,5000);

Now, how can I transform the buffer in a string so I can do a
regex.ismatch on the string???


As Nicolas pointed out, you must decode the the byte array using an
Encoding object or by wrapping the stream in a StreamReader -- although
I strongly disagree that ASCII is of any use here; you're better of
with ISO-8859-1 or UTF-8.

Note that your code above has two huge mistakes:

1. You read from the request stream. That should be the response stream.
2. You only read up to 5000 bytes. That will hardly cut it...

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #7
I am posting some code here just for your reference:

public static string MakeWebRequest(string url)
{
string output = null;
try
{
WebRequest request = WebRequest.Create(url);
request.Timeout = Settings.WarmUpTimeout;
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader (responseStream);
output = reader.ReadToEnd ();
reader.Close();
}
}
catch(Exception)
{
}
return output;
}
Ting Huang
"Ricardo Luceac" wrote:


HI all...

I'm doing the following to get a web page:

HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.google.com");
HttpWebResponse wresp = (HttpWebResponse)wr.GetResponse();
Stream wpage = wr.GetRequestStream();
byte[] buffer = new byte[5000];
wpage.Read(buffer,0,5000);

Now, how can I transform the buffer in a string so I can do a
regex.ismatch on the string???
thx...

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #8

Thanks... It's working now...

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #9

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

Similar topics

1
by: Michael Brennan-White | last post by:
If I submit my for using a get action the resulting page loads . If I use a post action I get an error page saying "The page cannot be found". I am calling the originating page!!! This happens...
0
by: Nathan | last post by:
Hi, I seem to having a peculiar problem with the display of odd and even pages in XSL-FO. Here is a small background of the problem. My xsl stylesheet mentions my fo:layout-master-set as ...
2
by: James | last post by:
I've been to websites where if I navigate off a form, trying to get back to it by hitting the back button gives me a page which says "Warning, page has expired". It doesn't display the page. I've...
4
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than...
2
by: John Lau | last post by:
Hi, Is there documentation that talks about the page lifecycle, the lifecycle of controls on the page, and the rendering of inline code, in a single document? Thanks, John
6
by: MooreSmnith | last post by:
When I navigate to the next page using Response.Rediect("MyNextPage.aspx") current page Page_Load event is called. What I may wrongly understood is that post back will happen whenever there is any...
1
by: Lenard Gunda | last post by:
Hi! I have the following problem. From my main page, when someone clicks a button, it uses client side javascript to open another .aspx page. This page displays content, based on what the...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
8
by: Ed Jay | last post by:
I want to use history.go() to navigate between my previously loaded pages. I'm looking for a way to trigger a function call when a page is accessed using history.go(). Is there an event generated?...
3
by: Mesut | last post by:
I have written a form in with radio buttons the name is set to orderby and the value is set to KundeVorName and the next value is KundeNachName and it goes so on. I wanna modify my query according...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.