System.Net 為支援網路通訊協定之Class Library

1. 主機名稱及IP位址

System.Net.EndPoint

System.Net.EndPointPermission

System.Net.IPAddress封裝一個特定的 IP 位址(為 Long 型別)。

Dim myIPAddress As IPAddress

myIPAddress = IPAddress.Parse("127.0.0.1")

'比較兩IP位址

Dim youIPAddress As IPAddress

...

If myIPAddress.Equals(youIPAddress) Then

...

'判斷IP位址是否為回送位址

If IPAddress.IsLoopback(myIPAddress) And _

    myIPAddress.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then

'為IPv4即回送位址

...

ElseIf IPAddress.IsLoopback(myIPAddress) And _

    myIPAddress.AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6 Then

'為IPv6即回送位址

...

End If

System.Net.IPEndPoint:其除了封裝一組特定的 IP 位址,還同時包含指定的通訊埠編號。

Dim myIPAddress As System.Net.IPAddress

Dim myIPEndPoint As System.Net.IPEndPoint

 

myIPAddress = IPAddress.Parse("127.0.0.1")

myIPEndPoint = New System.Net.IPEndPoint(myIPAddress, 80)


Dim strResult As String

strResult = "Address Family:" & myIPEndPoint.AddressFamily.ToString & " - " & _

"IP + Port:" & myIPEndPoint.Address.ToString & "+" & myIPENdPoint.Port.ToString

System.Net.IPHostEntry:裝載 Resolve 方法回傳之所有 IP 位址,即解析指定主機名稱的 IP 位址。

Try

    Dim strHostName As String
    strHostName = Dns.GetHostName
    txtHstName.Text = strHostName
    Dim myIPHostEntry As IPHostEntry = Dns.Resolve(strHostName)
    Dim myIPAddresses() As IPAddress = myIPHostEntry.AddressList
    Dim myIPAddress As IPAddress
    Dim strIPAddressList As String

    For Each myIPAddress In myIPAddresses
        strIPAddressList = strIPAddressList  + myIPAddress.ToString
    Next

    txtIPAddress.Text = strIPAddressList

    ' 反向解析主機名稱
    txtHostName.Text = Dns.GetHostByAddress(strIPAddressList).HostName

Catch ex As SocketException

    MessageBox.Show(ex.Message, "例外訊息", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

System.Net.SocketAddress

Dim myIPAddress As System.Net.IPAddress

Dim myIPEndPoint As System.Net.IPEndPoint

Dim mySocket As System.Net.SocketAddress

 

myIPAddress = IPAddress.Parse("127.0.0.1")

myIPEndPoint = New System.Net.IPEndPoint(myIPAddress, 80)

mySocket = myIPEndPoint.Serialize()


Dim strResult As String

strResult = "SocketAddress:" & mySocket.ToString()

'將 IPEndPoint 序列化為 SocketAddress 之內容為
InterNetwork:16:{0,80,127,0,0,1,0,0,0,0,0,0,0,0}

2. DNS (Domain Name System or Domain Name Service)

System.Net.Dns

Imports System.Net

Imports System.Net.Sockets

 

Dim hostEntry As IPHostEntry

Dim aliasList As String()

Dim addrList As IPAddress()


hostEntry = Dns.GetHostEntry(txtHost.text)

'可能有一個以上的 Alias

aliasList = hostEntry.Aliases

For i = 0 To aliasList.Length - 1

    aliasList(i).ToString

Next

'可能有一個以上的 IP Address

addrList = hostEntry.AddressList

For i = 0 To addrList.Length - 1

    addrList(i).ToString

Next

 

'取得 Local 主機的識別名稱

Dim localHostName as String

Dim localHostEntry as IPHostEntry

localHostName = Dns.GetHostName

localHostEntry = Dns.GetHostByName(localHostName)

txtHost.text = localHostEntry.HostName

 

'透過DNS找尋IP位址相對應之主機名稱

Dim ipAddr as IPAddress

Dim remoteHostEntry as IPHostEntry

ipAddr = IPAddress.Parse(txtIP.text)

remoteHostEntry = Dns.GetHostByAddress(ipAddr)

txtHost.text = remoteHostEntry.HostName

System.Net.DnsPermission

System.Net.DnsPermissionAttribute

3. URL (Uniform Resource Locators)、URI (Uniform Resource Identifiers)

<通訊協定>://<主機位址>/<虛擬目錄>/<資源>?<參數>

Base URL + Relative URL

System.Uri

Dim uri1 As Uri = New Uri("http://www.microsoft.com/Taiwan")

Dim uri2 As Uri = New Uri("http://www.microsoft.com.tw")

'判斷兩個URI是否相同

If uri1.Equals(uri2) Then

...

'判斷兩個URI的差異

uri1.MakeRelativeUri(uri2)

System.UriBuilder

4. 請求 Request 與回應Response

FileWebRequest & File WebResponse 處理 file:// 之 URL 通訊協定

FtpWebRequest & FtpWebResponse 處理 FTP 通訊協定

HttpWebRequest & HttpWebResponse 處理 HTTP 通訊協定

WebRequest & WebResponse 為抽象類別(均繼承此類別)

System.Net.FileWebRequest

System.Net.FileWebResponse

System.Net.FtpWebRequest

System.Net.FtpWebResponse

System.Net.GlobalProxySelection

System.Net.HttpVersion

System.Net.HttpWebRequest

System.Net.HttpWebResponse

System.Net.ProtocolViolationException

System.Net.ServicePoint

System.Net.WebClient

System.Net.WebException

System.Net.WebHeaderCollection

System.Net.WebProxy

System.Net.WebRequest

System.Net.WebResponse

5. HTTP Cookie

通常被儲存於 Windows 目錄下的 Temporary Internet Files 目錄中,其檔案名稱格式如下:

Cookie:<使用者名稱>@<網站的URL>

System.Net.Cookie

System.Net.CookieCollection

System.Net.CookieContainer

System.Net.CookieException

6. 網路組態設定

System.Net.Confuguration

7. 網路資訊

System.Net.NetworkInformation
請參考 VB.NET 2005取得本機網路卡的MAC Address

'Windows IP Configuration
Dim computerProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()

MessageBox.Show("Host Name .......................... : " + computerProperties.HostName + vbCrLf + _
          "Primary Dns Suffix .......................... : " + computerProperties.DomainName)

'Ethernet adapter
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()

Dim adapter As Net.NetworkInformation.NetworkInterface

For Each adapter In nics
    Dim properties As IPInterfaceProperties = adapter.GetIPProperties()

    MessageBox.Show(adapter.Description.ToString + vbCrLf + _
                    String.Empty.PadLeft(adapter.Description.Length, "="c) + vbCrLf + _
                    "  Interface type .......................... : " + adapter.NetworkInterfaceType + vbCrLf + _
                    "  Physical Address ........................ : " + adapter.GetPhysicalAddress().ToString() + vbCrLf + _
                    "  Is receive only.......................... : " + adapter.IsReceiveOnly.ToString + vbCrLf + _
                    "  Multicast................................ : " + adapter.SupportsMulticast.ToString)
Next adapter

8. 網路認證Authentication與權限Permission

System.Net.AuthenticationManager

System.Net.Authorization

System.Net.CredentialCache

System.Net.NetworkCredential

System.Net.SocketPermission

System.Net.SocketPermissionAttribute

System.Net.SocketPermissionAttribte

System.Net.WebPermission

System.Net.WebPermissionAttribute

tsuozoe 發表在 痞客邦 留言(0) 人氣()