Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HttpWebRequest error help please.

Reply
Thread Tools

HttpWebRequest error help please.

 
 
Bruce W.1
Guest
Posts: n/a
 
      12-26-2003
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 />";
}
}
}
 
Reply With Quote
 
 
 
 
Morgan
Guest
Posts: n/a
 
      12-26-2003
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.


"Bruce W.1" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 />";
> }
> }
> }



 
Reply With Quote
 
 
 
 
Joerg Jooss
Guest
Posts: n/a
 
      12-26-2003
"Morgan" wrote:

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

--
Joerg Jooss
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Joerg Jooss
Guest
Posts: n/a
 
      12-26-2003
"Bruce W.1" wrote:

> 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,
--
Joerg Jooss
(E-Mail Removed)
 
Reply With Quote
 
Feroze [MSFT]
Guest
Posts: n/a
 
      12-26-2003
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




"Bruce W.1" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 />";
> }
> }
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
401 Error with httpwebrequest =?Utf-8?B?dHduZXR5MG5lQHlhaG9vLmNvbQ==?= ASP .Net 1 10-13-2005 09:36 AM
HttpWebRequest is giving: "Error on HTTP request: The remote server returned an error: (403) Forbidden." edsuslen@yahoo.com ASP .Net 2 09-27-2005 12:17 PM
MSXML2.ServerXMLHTTP to HttpWebRequest: Error 500 Maris Janis Vasilevskis ASP .Net 2 08-29-2005 08:03 PM
HttpWebRequest 404 Error Randy Chow ASP .Net 1 10-28-2004 05:35 AM
HttpWebRequest.GetResponse returns 404 No Found error warlord ASP .Net 12 04-21-2004 08:54 AM



Advertisments