how to get a web page

R

Ricardo Luceac

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...
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
R

Ricardo Luceac

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...
 
N

Nicholas Paldino [.NET/C# MVP]

Ricardo,

Are you trying to read the contents twice? Or perhaps you have disposed
of the response already?
 
R

Ricardo Luceac

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...
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 
J

Joerg Jooss

Ricardo said:
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,
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top