Hi .... can anyone help?
I have a piece of code that checks to see if a connection exists but am experiencing some issues as the website is recognised as "Not Authorised" (specifically a 401 error). Please note the code works perfectly for http://www.amazon.com" onclick="window.open(this.href);return false; etc. I know the website exists if I get the error but I am struggling to know how to trap the error as I am unable to do this within a Try/Catch (I have to admit the code has been supplied by a very kind indivdiual who posted it on the internet). The code is below:
Dim url As New System.Uri("http://learningguide/")
Dim req As System.Net.WebRequest
req = System.Net.WebRequest.Create(url)
Dim resp As System.Net.WebResponse
Try
resp = req.GetResponse()
resp.Close()
req = Nothing
MsgBox("Website Found!")
IsConnectionAvailable = True
Catch ex As Exception
req = Nothing
MsgBox("Website not found. Check the url and internet connection")
IsConnectionAvailable = False
End Try
If someone can indicate where I can place the error trap I would be very grateful. I am aware that once "resp = req.GetResponse" runs, the 401 error appears then the code jumps to "Catch ex as Exception".
Thanks in advance
Jack
Check Connection - Website (VB.Net)
-
- Administrator
- Posts: 78665
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Check Connection - Website (VB.Net)
I'm not familiar with VB.Net so I can't help with the technical side, but shouldn't the URL be http://learningguide.com" onclick="window.open(this.href);return false; or http://learningguide.co.uk" onclick="window.open(this.href);return false; or something like that?
Best wishes,
Hans
Hans
-
- 2StarLounger
- Posts: 107
- Joined: 23 Mar 2010, 13:42
Re: Check Connection - Website (VB.Net)
Hi Hans
I've looked into this further and am now aware that I can trap the Exception (Catch ex as Exception), so I'll take a look and see if I can resolve the issue.
Thanks as always
Jack
I've looked into this further and am now aware that I can trap the Exception (Catch ex as Exception), so I'll take a look and see if I can resolve the issue.
Thanks as always
Jack
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Check Connection - Website (VB.Net)
Hello. From my vast knowledge of VB.Net (he, he), Exception catches all Exceptions, but the error you receive might be something other than a website not being found, perhaps some security warning/message.
So you need to find out, easiest by debugging/stepping through the code, the specific Exception Class and Type that occurs for the site. Then you can catch this specific Exception - before the catch all Exception.
Exceptions have a Message property that you can display, but it's really the Exception Class/Type that you need to discover. When you are debugging within the Catch clause I believe that you can just hover the mouse over 'ex' to discover this information.
Hope I've been of some (little) help. Andy.
So you need to find out, easiest by debugging/stepping through the code, the specific Exception Class and Type that occurs for the site. Then you can catch this specific Exception - before the catch all Exception.
Code: Select all
Catch ex As SomeExceptionClass.SpecificException
'need to first find out the particular Exception that occurred
IsConnectionAvailable = True 'possibly..
Catch ex As Exception
req = Nothing
MsgBox(ex.Message)
Stop 'enter debugging mode so you can determine the Exception Class and Type
IsConnectionAvailable = False
End Try
Hope I've been of some (little) help. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Check Connection - Website (VB.Net)
Hello. The particular Exception raised has a Response and a Status property. The Response doesn't seem useful.
You could move 'req = Nothing' into the Finally clause so that it will ALWAYS be called, but in .Net it's generally not essential (because of Garbage Collection).
Andy.
Code: Select all
Dim IsConnectionAvailable As Boolean = False
Dim url As New System.Uri("http://learningguide/")
Dim req As System.Net.WebRequest
req = System.Net.WebRequest.Create(url)
Dim resp As System.Net.WebResponse
Try
resp = req.GetResponse()
resp.Close()
req = Nothing
MsgBox("Website Found!")
IsConnectionAvailable = True
Catch ex As System.Net.WebException
If ex.Status.ToString.Contains("NameResolutionFailure") Then
MsgBox("Please check that the web address is correctly typed.")
IsConnectionAvailable = False
End If
Catch ex As Exception
MsgBox("Website not found. Check the url and internet connection")
IsConnectionAvailable = False
Finally
req = Nothing 'not normally necessary in VB.Net
End Try
End Sub
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- 2StarLounger
- Posts: 107
- Joined: 23 Mar 2010, 13:42
Re: Check Connection - Website (VB.Net)
Thanks for your help Andy .... all sorted and running like a dream.