網路程式設計 - C# 版 日期 : 2018/12/10
範例 4-1 : IPAddress.Parse IPAddress ipAddr1 = IPAddress.Parse(txtIP1.Text); 2 陳鍾誠 - 2018/12/10
範例 4-2 : IsLoopback if (IPAddress.IsLoopback(address) && address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // 為IPv4及回送位址 …. else if (IPAddress.IsLoopback(address) && address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) // 為IPv6及回送位址 ….. 3 陳鍾誠 - 2018/12/10
範例 4-3 : IPEndPoint IPAddress address = IPAddress.Parse(txtIP.Text); IPEndPoint ipEndPoint = new System.Net.IPEndPoint(address, Int32.Parse(txtPort.Text)); // 將IPEndPoint序列化為SocketAddress SocketAddress socketAddr = ipEndPoint.Serialize(); 4 陳鍾誠 - 2018/12/10
範例 4-4 : Dns.GetHostEntry IPHostEntry hostEntry = Dns.GetHostEntry(txtHost.Text); // 由於主機有可能有一個以上的 Alias // 因此程式中以迴圈方式判斷 Aliases aliasList = hostEntry.Aliases; for (int i = 0; i <= aliasList.Length - 1; i++) strTemp = strTemp + aliasList[i].ToString() + " "; txtAlias.Text = strTemp; // 由於主機有可能有一個以上的 IP Address // 因此程式中以迴圈方式判斷 AddressList IPAddress[] addrList = hostEntry.AddressList; for (int i = 0; i <= addrList.Length - 1; i++) strTemp = strTemp + addrList[i].ToString() + " "; txtIP.Text = strTemp; 5 陳鍾誠 - 2018/12/10
範例 4-5 : Dns.GetHostName() // 取得Local主機的識別名稱 string localHostName = Dns.GetHostName() ; TextBox1.Text = localHostName ; 6 陳鍾誠 - 2018/12/10
範例 4-6 : GetHostName localHostName = Dns.GetHostName(); localHostEntry = Dns.GetHostByName(localHostName); txtHost.Text = localHostEntry.HostName; // 由於主機有可能有一個以上的 Alias // 因此程式中以迴圈方式判斷 Aliases aliasList = localHostEntry.Aliases; for (int i = 0; i <= aliasList.Length - 1; i++) strTemp = strTemp + aliasList[i].ToString() + " "; txtAlias.Text = strTemp; // 由於主機有可能有一個以上的 IP Address // 因此程式中以迴圈方式判斷 AddressList addrList = localHostEntry.AddressList; for (int i = 0; i <= addrList.Length - 1; i++) strTemp = strTemp + addrList[i].ToString() + " "; txtIP.Text = strTemp; 7 陳鍾誠 - 2018/12/10
範例 4-7 : GetHostByName remoteHostEntry = Dns.GetHostByName(txtHost.Text); // 由於主機有可能有一個以上的 Alias // 因此程式中以迴圈方式判斷 Aliases aliasList = remoteHostEntry.Aliases; for (int i = 0; i <= aliasList.Length - 1; i++) strTemp = strTemp + aliasList[i].ToString() + " "; txtAlias.Text = strTemp; strTemp = ""; // Add by CCC // 由於主機有可能有一個以上的 IP Address // 因此程式中以迴圈方式判斷 AddressList addrList = remoteHostEntry.AddressList; for (int i = 0; i <= addrList.Length - 1; i++) strTemp = strTemp + addrList[i].ToString() + " "; txtIP.Text = strTemp; 8 陳鍾誠 - 2018/12/10
範例 4-8 : GetHostByAddress ipAddr = IPAddress.Parse(txtIP.Text); // 透過DNS找尋IP位址相對應之主機名稱 remoteHostEntry = Dns.GetHostByAddress(ipAddr); txtHost.Text = remoteHostEntry.HostName; 9 陳鍾誠 - 2018/12/10
範例 4-9 : Dns.Resolve(host) IPHostEntry hostEntry = Dns.Resolve(txtIP.Text); IPAddress[] addrList = hostEntry.AddressList; strTemp = ""; for (int i = 0; i <= addrList.Length - 1; i++) strTemp = strTemp + addrList[i].ToString() + "\r\n"; MessageBox.Show("Address List: " + "\r\n" + strTemp, "DNS Resolve", essageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) ; 10 陳鍾誠 - 2018/12/10
範例 4-10 : URI System.Uri URL = null; string sResult = ""; URL = new System.Uri(txtURL.Text); // System.Uri類別之屬性 sResult = "AbsolutePath: " + URL.AbsolutePath + "\r\n"; sResult = sResult + "AbsoluteUri: " + URL.AbsoluteUri + "\r\n"; sResult = sResult + "Authority: " + URL.Authority + "\r\n"; sResult = sResult + "Host: " + URL.Host + "\r\n"; sResult = sResult + "Port: " + URL.Port + "\r\n"; sResult = sResult + "LocalPath: " + URL.LocalPath + "\r\n"; sResult = sResult + "IsDefaultPort: " + URL.IsDefaultPort + "\r\n"; sResult = sResult + "IsFile: " + URL.IsFile + "\r\n"; sResult = sResult + "PathAndQuery: " + URL.PathAndQuery + "\r\n"; sResult = sResult + "Query: " + URL.Query + "\r\n"; sResult = sResult + "Scheme: " + URL.Scheme + "\r\n"; sResult = sResult + "UserEscaped: " + URL.UserEscaped + "\r\n"; sResult = sResult + "UserInfo: " + URL.UserInfo + "\r\n"; 11 陳鍾誠 - 2018/12/10
範例 4-11:Cookie string strBuff = ""; System.Net.Cookie cookie = new System.Net.Cookie("HASH", "", "/", "microsoft.com"); strBuff = "Comment: " + cookie.Comment.ToString() + "\r\n"; strBuff = strBuff + "Domain: " + cookie.Domain.ToString() + "\r\n"; strBuff = strBuff + "Expired: " + cookie.Expired.ToString() + "\r\n"; strBuff = strBuff + "Expires: " + cookie.Expires.ToString() + "\r\n"; strBuff = strBuff + "Name: " + cookie.Name.ToString() + "\r\n"; strBuff = strBuff + "Path: " + cookie.Path.ToString() + "\r\n"; strBuff = strBuff + "Port: " + cookie.Port.ToString() + "\r\n"; strBuff = strBuff + "Secure: " + cookie.Secure.ToString() + "\r\n"; strBuff = strBuff + "Value: " + cookie.Value.ToString() + "\r\n"; strBuff = strBuff + "Version: " + (cookie.Version == 1 ? "2109" : "2965") + "\r\n"; txtCookie.Text = strBuff; 12 陳鍾誠 - 2018/12/10
範例 4-12 : Web Request 陳鍾誠 - 2018/12/10 // 以WebRequest抽象類別的Create方法建立WebRequest物件 request = WebRequest.Create(new Uri(txtURL.Text)); // WebRequest類別之屬性 // 連結群組名稱 if (request.ConnectionGroupName != null) result = result + "ConnectionGroupName: " + request.ConnectionGroupName.ToString() + "\r\n"; // 用戶端所傳送資料內容的大小(byte) if (request.ContentLength != -1) result = result + "ContentLength: " + request.ContentLength.ToString() + "\r\n"; … // 是否要求預先驗證 result = result + "PreAuthenticate: " + request.PreAuthenticate + "\r\n"; // 用戶端所傳送的URI result = result + "RequestUri: " + request.RequestUri.ToString() + "\r\n"; txtRequest.Text = result; 13 陳鍾誠 - 2018/12/10
範例 4-13 : Web Response 陳鍾誠 - 2018/12/10 // 以WebRequest抽象類別的Create方法建立WebRequest物件 request = WebRequest.Create(new Uri(txtURL.Text)); // 使用WebRequest類別的GetResponse方法建立WebResponse物件 response = request.GetResponse(); // WebResponse類別之屬性 // 用戶端所接收資料內容的大小(byte) result = result + "ContentLength: " + response.ContentLength.ToString() + "\r\n"; // 用戶端所接收資料內容的MIME格式 result = result + "ContentType: " + response.ContentType.ToString() + "\r\n"; // 用戶端所接收的URI result = result + "ResponseUri: " + response.ResponseUri.ToString() + "\r\n"; // 取得伺服端回應串流 System.IO.Stream respStream = response.GetResponseStream(); // 設定編碼為UTF-8並取得伺服端回應串流的內容 StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8); string strBuff = ""; char[] cbuffer = new char[256]; int byteRead = respStreamReader.Read(cbuffer, 0, 256); while (byteRead != 0) { string strResp = new string(cbuffer, 0, byteRead); strBuff = strBuff + strResp; byteRead = respStreamReader.Read(cbuffer, 0, 256); } respStream.Close(); // 關閉回應串流 response.Close(); result = result + "\r\nResponse: \r\n\r\n" + strBuff; txtResponse.Text = result; 14 陳鍾誠 - 2018/12/10
IPAddress 範例 using System; using System.Net; class IPAddressTest { static void Main() { IPAddress ipAddr = IPAddress.Parse("210.59.154.30"); Console.WriteLine("ipAddr="+ipAddr); IPEndPoint ipEndPoint = new System.Net.IPEndPoint(ipAddr, 80); Console.WriteLine("ipEndPoint=" + ipEndPoint); // 將IPEndPoint序列化為SocketAddress SocketAddress socketAddr = ipEndPoint.Serialize(); Console.WriteLine("socketAddr=" + socketAddr); } 15 陳鍾誠 - 2018/12/10
Dns 範例 using System; using System.Net; class DnsTest { static void Main(String[] args) IPHostEntry hostEntry = Dns.GetHostEntry(args[0]); // 由於主機有可能有一個以上的 Alias // 因此程式中以迴圈方式判斷 Aliases string[] aliasList = hostEntry.Aliases; for (int i = 0; i <= aliasList.Length - 1; i++) Console.WriteLine("Alias "+i+" : "+aliasList[i]); // 由於主機有可能有一個以上的 IP Address // 因此程式中以迴圈方式判斷 AddressList IPAddress[] addrList = hostEntry.AddressList; for (int i = 0; i <= addrList.Length - 1; i++) Console.WriteLine("Address " + i + " : " + addrList[i]); } 16 陳鍾誠 - 2018/12/10
IpToHost using System; using System.Net; using System.Net.Sockets; class IpToHost { static void Main(String[] args) IPAddress ipAddr = IPAddress.Parse(args[0]); // 透過DNS找尋IP位址相對應之主機名稱 IPHostEntry remoteHostEntry = Dns.GetHostEntry(ipAddr); Console.WriteLine("host of ip " + ipAddr + " is " + remoteHostEntry.HostName); } 17 陳鍾誠 - 2018/12/10
UrlParseTest using System; using System.Net; class UrlParseTest { static void Main(String[] args) { System.Uri URL = null; URL = new System.Uri(args[0]); // System.Uri類別之屬性 Console.WriteLine("AbsolutePath: " + URL.AbsolutePath); Console.WriteLine("AbsoluteUri: " + URL.AbsoluteUri); Console.WriteLine("Authority: " + URL.Authority); Console.WriteLine("Host: " + URL.Host); Console.WriteLine("Port: " + URL.Port); Console.WriteLine("LocalPath: " + URL.LocalPath); Console.WriteLine("IsDefaultPort: " + URL.IsDefaultPort); Console.WriteLine("IsFile: " + URL.IsFile); Console.WriteLine("PathAndQuery: " + URL.PathAndQuery); Console.WriteLine("Query: " + URL.Query); Console.WriteLine("Scheme: " + URL.Scheme); Console.WriteLine("UserEscaped: " + URL.UserEscaped); Console.WriteLine("UserInfo: " + URL.UserInfo); }} 18 陳鍾誠 - 2018/12/10