HttpWebRequest error help please.

Discussion in 'ASP .Net' started by Bruce W.1, Dec 26, 2003.

  1. Bruce W.1

    Bruce W.1 Guest

    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 />";
    }
    }
    }
    Bruce W.1, Dec 26, 2003
    #1
    1. Advertising

  2. Bruce W.1

    Morgan Guest

    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" <> wrote in message
    news:...
    > 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 />";
    > }
    > }
    > }
    Morgan, Dec 26, 2003
    #2
    1. Advertising

  3. Bruce W.1

    Joerg Jooss Guest

    "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
    Joerg Jooss, Dec 26, 2003
    #3
  4. Bruce W.1

    Joerg Jooss Guest

    "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
    Joerg Jooss, Dec 26, 2003
    #4
  5. 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" <> wrote in message
    news:...
    > 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 />";
    > }
    > }
    > }
    Feroze [MSFT], Dec 26, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sojwal

    HttpWebRequest problem: Please help

    Sojwal, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    457
    Feroze [msft]
    Nov 10, 2004
  2. Sojwal

    HttpWebRequest problem: Please help

    Sojwal, Nov 3, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,963
    Sojwal
    Nov 4, 2004
  3. Amil
    Replies:
    4
    Views:
    12,908
    bboyse
    May 5, 2011
  4. japslam japslam via DotNetMonster.com

    Please help !! Problem about HttpWebRequest - GetResponse()

    japslam japslam via DotNetMonster.com, Apr 22, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    10,877
    Bruce Barker
    Apr 22, 2005
  5. KK
    Replies:
    2
    Views:
    512
    Big Brian
    Oct 14, 2003
Loading...

Share This Page