|
-
Jan 28th, 2007, 04:02 PM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] Check for internet connection?
Hello!
When my application loads, it pings a website, and downloads an .xml file to check for a newer version of the program. However, i quickly found out, this resulted in an error if either a firewall is blocking this, or if the internet connection is disconnected. So, i put in some "if"'s and "else"'s. This partially solved the problem.
But on computers with no internet connection at all (as in not disconnected connections, but with no connections), an unhandled exception is thrown when the ping is run as there are no connections present. So is there a way to fix this, so that if there are no connections available, it just skips the whole thing?
Some code
VB Code:
If My.Computer.Network.Ping("http://connect-utb.byethost8.com", 1000) Then My.Computer.Network.DownloadFile(remotefile, localfile)
Else
End if
-
Jan 28th, 2007, 05:05 PM
#2
Re: [2005] Check for internet connection?
Here is some code that detects whether a URL is accessible, its best to check two or three well known sites (such as google, yahoo etc.) in order to determine if a connection is available.
VB Code:
Public Function CheckURLExists(ByVal site As String) As Boolean
Dim objUrl As New System.Uri(site)
Dim objWebReq As System.Net.WebRequest
Dim objResp As System.Net.WebResponse
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim Success As Boolean
Try
objResp = objWebReq.GetResponse
objResp.Close()
Success = True
Catch ex As Exception
Success = False
End Try
objWebReq = Nothing
Return Success
End Function
Note that there is an oddity with this function, I'm not sure whether this is a bug in the IDE or what, but it always catches an exception correctly when run after a build, but the try/catch does not always catch an exception when run within the IDE.
Last edited by Bulldog; Jan 28th, 2007 at 05:11 PM.
-
Jan 28th, 2007, 08:38 PM
#3
Addicted Member
Re: [2005] Check for internet connection?
Put the code in a Try/Catch statement?
Since the code given above has an oddity about it when run in the IDE, why not just replace it with your ping code?
-
Jan 28th, 2007, 08:58 PM
#4
Hyperactive Member
Re: [2005] Check for internet connection?
This is what I do:
VB Code:
Private Function internetConnection() As Boolean
Dim url As New System.Uri("http://www.yoursite.com/")
Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
Dim response As System.Net.WebResponse = request.GetResponse()
Try
response.Close()
request = Nothing : Return True
Catch ex As Exception
request = Nothing : Return False
End Try
End Function
Syntax:
VB Code:
If internetConnection() = True Then
Label1.Text = "Connected"
Else : Label1.Text = "Not Connected"
End If
Last edited by BrendanDavis; Jan 28th, 2007 at 09:01 PM.
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
-
Feb 5th, 2007, 06:19 PM
#5
Thread Starter
Lively Member
Re: [2005] Check for internet connection?
Thanks for reply, but sadly, this did not solve my problem. Again, if my program is used on a pc with no internet connection available what so ever (not disconnected or connect, just no internet connections at all, as in "Network Connections" showing up empty.. just to get the point through), an unhandled exception is thrown.
EDIT: This also happens if the internet connection is disabled..
Anyone?
Cheers!
Last edited by Untouchab1e; Feb 5th, 2007 at 06:24 PM.
-
Feb 5th, 2007, 06:24 PM
#6
Addicted Member
Re: [2005] Check for internet connection?
I suggested the Try/Catch statement. If the user has the condition which you describe, then you can fairly assume they don't have internet access. If the code you're trying is causing an error when the condition you describe occurs, then a Try/Catch statement should work? Are you using a Try/Catch statement?
-
Feb 5th, 2007, 06:26 PM
#7
Thread Starter
Lively Member
Re: [2005] Check for internet connection?
Im using the Code BrendanDavis posted
VB Code:
Try
response.Close()
request = Nothing : Return True
Catch ex As Exception
request = Nothing : Return False
End Try
So yes, im using a try/catch statement..
I get the following error when trying to run it with no internet connection:
The remote name could not be resolved
Last edited by Untouchab1e; Feb 5th, 2007 at 06:32 PM.
-
Feb 5th, 2007, 06:32 PM
#8
Addicted Member
Re: [2005] Check for internet connection?
Exactly which line causes your error?
-
Feb 5th, 2007, 06:33 PM
#9
Thread Starter
Lively Member
Re: [2005] Check for internet connection?
VB Code:
Dim response As System.Net.WebResponse = request.GetResponse()
-
Feb 5th, 2007, 06:37 PM
#10
Addicted Member
Re: [2005] Check for internet connection?
Try moving that line to within the Try/Catch statement. If you're using his code exactly, it's outside of the Try/Catch...
EDIT: For that matter, try moving the entire bit of the function code (all the declarations) to within the try/catch statement.
-
Feb 5th, 2007, 06:39 PM
#11
Thread Starter
Lively Member
Re: [2005] Check for internet connection?
That solved it! Thank you so much!
-
Feb 5th, 2007, 06:43 PM
#12
Addicted Member
Re: [RESOLVED] [2005] Check for internet connection?
Glad to finally be able to help someone instead of being the one bening helped.
For my future copy-and-pasting:
VB Code:
Private Function internetConnection() As Boolean
Dim url As New System.Uri("http://www.yoursite.com/")
Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
Dim response As System.Net.WebResponse = request.GetResponse()
Try
Dim url As New System.Uri("http://www.yoursite.com/")
Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
Dim response As System.Net.WebResponse = request.GetResponse()
response.Close()
request = Nothing : Return True
Catch ex As Exception
request = Nothing : Return False
End Try
End Function
VB Code:
If internetConnection() = True Then
Label1.Text = "Connected"
Else : Label1.Text = "Not Connected"
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|