PDA

View Full Version : بررسی Run Time بر خط بودن اینترنت



پرستو پارسایی
دوشنبه 17 آذر 1399, 00:13 صبح
با سلام من میخوام بهترین روش رو برای بررسی برخط بودن اینترنت بکار ببرم از این 4 متد هم استفاده کردم . سوالم اینه بهترین متد کدام هست و آیا این متد ها در تمام ویندوزها قابل اجرا هست ؟ مخصوصا 8 و 10 و اگر روش بهتری رو دوستان استفاده میکنند ممنونم میشم راهنمایی بفرمائید سپاسگزارم

Imports System.RuntimeImports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Net 'IPHostEntry, HttpWebRequest & HttpWebResponse in Method 4 & 2
Imports System.Net.Sockets 'TCP Client in Method 3


Public Class frmIDetect
'Used in Method 1
'Retrieves the connected state of the local system
<DllImport("wininet.dll")> _
Private Shared Function InternetGetConnectedState(ByRef Description As Integer, ByVal ReservedValue As Integer) As Boolean
End Function




Public Shared Function Method1() As Boolean
Try
Dim ConnDesc As Integer 'Return value
MessageBox.Show("Connected")
Return InternetGetConnectedState(ConnDesc, 0) 'Return result
Catch
Return False 'Not connected
End Try


End Function


Private Function Method2() As Boolean
Try
Dim iheObj As IPHostEntry = Dns.GetHostByName("www.Google.com") 'Gets the DNS information for the specified DNS host name
MessageBox.Show("Connected")


Return True
Catch
Return False 'Not connected
End Try
End Function


Private Function Method3() As Boolean
Try
Dim tcpClient As New TcpClient("www.Google.com", 80) 'Provides client connections for TCP network services
tcpClient.Close()
MessageBox.Show("Connected")
Return True


Catch ex As System.Exception
Return False 'Not connected
End Try
End Function


Public Shared Function Method4(ByVal url As String) As Boolean
Dim hwrRequest As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) 'Makes a request to a Uniform Resource Identifier (URI)
hwrRequest.Method = "HEAD" 'Read if there is a Head section
Try
Dim hwrResponse As HttpWebResponse = DirectCast(hwrRequest.GetResponse(), HttpWebResponse) 'Provides a response from a Uniform Resource Identifier (URI)
hwrResponse.Close()
MessageBox.Show("Connected")
Return True
Catch
Return False
End Try
End Function


Private Sub btnMethod1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMethod1.Click
'Method 1
Method1()
End Sub


Private Sub btnMethod2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMethod2.Click
'Method 2
Method2()
End Sub


Private Sub btnMethod3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMethod3.Click
'Method 3
Method3()
End Sub


Private Sub btnMethod4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMethod4.Click
'Method 4
Method4("http://www.Google.com")
End Sub
End Class