用api判断:
vb.net的代码Public Module GetInternetState
’ Declare the API function.
Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long
’ Define the possible types of connections.
Private Enum ConnectStates
LAN = &H2
Modem = &H1
Proxy = &H4
Offline = &H20
Configured = &H40
RasInstalled = &H10
End Enum
Public Sub Main()
’ Get the connected status.
Dim dwFlags As Long
Dim Connected As Boolean = (InternetGetConnectedState(dwFlags, 0&) <> 0)
If Connected Then
Console.WriteLine("This computer is connected to the Internet.")
’ Display all connection flags.
Console.Write("Connection flags:")
Dim ConnectionType As ConnectStates
For Each ConnectionType In System.Enum.GetValues(GetType(ConnectStates))
If (ConnectionType And dwFlags) = ConnectionType Then
Console.Write(" " & ConnectionType.ToString())
End If
Next
End If
Console.ReadLine()
End Sub End Module
A sample output is shown here:
This computer is connected to the Internet.Connection flags: LAN
vb.net的代码Public Module GetInternetState
’ Declare the API function.
Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long
’ Define the possible types of connections.
Private Enum ConnectStates
LAN = &H2
Modem = &H1
Proxy = &H4
Offline = &H20
Configured = &H40
RasInstalled = &H10
End Enum
Public Sub Main()
’ Get the connected status.
Dim dwFlags As Long
Dim Connected As Boolean = (InternetGetConnectedState(dwFlags, 0&) <> 0)
If Connected Then
Console.WriteLine("This computer is connected to the Internet.")
’ Display all connection flags.
Console.Write("Connection flags:")
Dim ConnectionType As ConnectStates
For Each ConnectionType In System.Enum.GetValues(GetType(ConnectStates))
If (ConnectionType And dwFlags) = ConnectionType Then
Console.Write(" " & ConnectionType.ToString())
End If
Next
End If
Console.ReadLine()
End Sub End Module
A sample output is shown here:
This computer is connected to the Internet.Connection flags: LAN
try
{
XMLHTTP http = new XMLHTTP();
http.open("GET", "http://www.sina.com.cn", false, null, null);
http.send(null);}
catch(Exception ex)
{
Console.WriteLine(ex.toString());
}