HttpWebRequest error help please.

B

Bruce W.1

I wrote this aspx link checker page. In it is a textbox where the user
enters a URL. This app then goes out and extracts all the links from
the webpage for the given URL and puts them in an arrayList of strings.

Then it checks each URL string to see if it's good. Below is the method
that is supposed to do this. It however gives the following error:
The underlying connection was closed: The remote name could not be
resolved.

Each URL string in the arrayList looks like this:
href="http://www.whatever.com"

The error may be caused by a URL that's no good, and that's fine. But
this method should skip over bad URL's and go on to the next one, rather
than making the whole thing croak.

How can I gracefully recover from errors and move on to the next URL?
Thanks for your help.

Code:

protected void CheckLinks(ArrayList urlA)
{
foreach(string s in urlA)
{
string x = s.Substring(6);
int n = x.Length;
string v = x.Substring(0, n-1);
resultLiteral.Text += "Checking link: " + v + "<br />";

String result;
WebResponse objResponse;

StreamReader sr; // = new StreamReader();
WebRequest objRequest = System.Net.HttpWebRequest.Create(v);
try
{
objResponse = objRequest.GetResponse();
sr = new StreamReader(objResponse.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();
}
catch (Exception e)
{
resultLiteral.Text += "Failure: " + e.ToString();
result = "";
Trace.Warn(e.ToString());
}
finally
{
}

if(!result.Equals(""))
{
resultLiteral.Text += "Successful<br />";
}
else
{
resultLiteral.Text += "Failure<br />";
}
}
}
 
M

Morgan

Have you tried moving the foreach block below your try..catch block?
Instead of:
foreach(string s in urla)
try
catch
finally

Use:
try
foreach(string s in urla)
catch
finally

Seems as though this would ensure all items will be processed inside of the
try..catch, rather than failing the routine on the first failure.
 
J

Joerg Jooss

Morgan said:
Have you tried moving the foreach block below your try..catch block?
Instead of:
foreach(string s in urla)
try
catch
finally

Use:
try
foreach(string s in urla)
catch
finally

Seems as though this would ensure all items will be processed inside
of the try..catch, rather than failing the routine on the first
failure.

No it won't. It exits the foreach loop on the first exception.
 
J

Joerg Jooss

Bruce W.1 said:
I wrote this aspx link checker page. In it is a textbox where the
user enters a URL. This app then goes out and extracts all the
links from the webpage for the given URL and puts them in an
arrayList of strings.

Then it checks each URL string to see if it's good. Below is the
method that is supposed to do this. It however gives the following
error: The underlying connection was closed: The remote name could
not be resolved.

Each URL string in the arrayList looks like this:
href="http://www.whatever.com"

The error may be caused by a URL that's no good, and that's fine.
But this method should skip over bad URL's and go on to the next
one, rather than making the whole thing croak.

The problem is that you do not create the WebRequest instance within the
try/catch block. If

WebRequest objRequest = System.Net.HttpWebRequest.Create(v);

throws an exception due to a malforemd URL, the foreach loop is
terminated.

Cheers,
 
F

Feroze [MSFT]

It might be that the proxy settings are not correct. Add a

req.Proxy = new WebProxy(http://foo, true);

and see if that makes the difference.

--
Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top