473,473 Members | 4,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HttpWebRequest error help please.

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 />";
}
}
}
Nov 18 '05 #1
4 1598
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" <br***@noDirectEmail.com> wrote in message
news:3F***************@noDirectEmail.com...
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 />";
}
}
}

Nov 18 '05 #2
"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
jo*********@gmx.net
Nov 18 '05 #3
"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
jo*********@gmx.net
Nov 18 '05 #4
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" <br***@noDirectEmail.com> wrote in message
news:3F***************@noDirectEmail.com...
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 />";
}
}
}

Nov 18 '05 #5

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

Similar topics

0
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not...
9
by: Mike Cronin via DotNetMonster.com | last post by:
Hi there, Can anyone tell me what level of encryption is used when making an HTTPS POST request through an instance of the System.Net.HttpWebRequest object? Thanks much in advance! Mike...
3
by: Jason | last post by:
I'm having a hard time getting a call to HttpWebRequest's GetRequestSteam to work. Each time I try to run it, I get the following error: The underlying connection was closed: Unable to...
2
by: GlennLanier | last post by:
Hello, I've searched the forums and can't find an answer -- if it i there, kindly point me in that direction. I would like to simulate a browser POSTing a FORM and be able to pars the response....
7
by: gorkos | last post by:
Hi, I am two days trying to solve a problem with some pages, which i get through HTTPWebRequest. Error is that some pages need Script to be enabled. But how to do this in HTTPWebRequest class?
7
by: Tom | last post by:
Hello all: I have a method that does a POST to a secured website using HttpWebRequest. It worked when logging in the site, but it failed with an HTTP prococol violation error when it is used to...
0
by: boxboy | last post by:
Hi, I'm writing a console application and am having a problem with HttpWebRequest when posting data to a webserver. A "System.Net.WebException: The server committed a protocol violation" is always...
3
by: JansenH | last post by:
We have implemented a 'HTTP Post' client in C# that posts Xml documents to a webserver. This is working fine if the post rate is one post for every 20 seconds. But if the post rate is increased to...
4
by: Natalia | last post by:
Hello, I need to provide the ability to post file and some form elements via our website (asp.net) to the third party website (asp page). On http://aspalliance.com/236#Page4 - I found great...
2
by: =?Utf-8?B?TGFycnlLdXBlcm1hbg==?= | last post by:
Our WebDev team seems to have found a problem that exposes a bug in .NET 2.0. This problem can be shown when trying to access a WebService using SSL and through a proxy server after using the...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.