C# 的 Web 相關物件之使用 日期 : 2018/11/21
範例 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; 2 陳鍾誠 - 2018/11/21
範例 4-12 : Web Request 陳鍾誠 - 2018/11/21 // 以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; 3 陳鍾誠 - 2018/11/21
範例 4-13 : Web Response 陳鍾誠 - 2018/11/21 // 以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; 4 陳鍾誠 - 2018/11/21
CookieTest 陳鍾誠 - 2018/11/21 using System; using System.Net; class CookieTest { static void Main(String[] args) System.Net.Cookie cookie = new System.Net.Cookie("HASH", "", "/", "microsoft.com"); String strBuff = ""; 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"; Console.WriteLine(strBuff); } 5 陳鍾誠 - 2018/11/21
WebCacheTest 陳鍾誠 - 2018/11/21 using System; using System.Net; using System.Net.Cache; class WebCacheTest { static void Main(String[] args) // 以WebRequest抽象類別的Create方法建立WebRequest物件 System.Net.WebRequest request = WebRequest.Create(new Uri(args[0])); // 設定快取原則(Cache Policy) // 若有符合用戶端請求之Cache資源,則使用此Cache滿足用戶端之請求 // 否則將用戶端請求傳送至伺服端 request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable); // 使用WebRequest類別的GetResponse方法建立WebResponse物件 System.Net.WebResponse response = request.GetResponse(); // WebResponse類別之屬性 // 用戶端所接收資料內容的大小(byte) String 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"; // 用戶端所接收之回應內容是否從快取中取得 result = result + "伺服端回應是否取自Cache? " + response.IsFromCache; // 關閉回應串流 response.Close(); Console.WriteLine(result); } 6 陳鍾誠 - 2018/11/21
WebClientTest 陳鍾誠 - 2018/11/21 using System; using System.Net; using System.IO; using System.Text; class WebClientTest { static void Main(String[] args) string url = "http://ccc.kmit.edu.tw/ccc/me/ccc.jpg"; string localfile = "ccc.jpg"; // 建立WebClient物件 WebClient webclient = new WebClient(); // 下載檔案 try // 自指定URI下載資料,並儲存為本機之檔案 webclient.DownloadFile(url, localfile); Console.WriteLine("Download file completed."); } catch (WebException ex) Console.WriteLine(ex.StackTrace.ToString()); webclient.Dispose(); 7 陳鍾誠 - 2018/11/21
WebRequestTest 陳鍾誠 - 2018/11/21 using System; using System.Net; class WebRequestTest { static void Main(String[] args) { String result = ""; // 以WebRequest抽象類別的Create方法建立WebRequest物件 WebRequest request = WebRequest.Create(new Uri("http://www.google.com/")); // WebRequest類別之屬性 if (request.ConnectionGroupName != null) // 連結群組名稱 result = result + "ConnectionGroupName: " + request.ConnectionGroupName.ToString() + "\r\n"; if (request.ContentLength != -1) // 用戶端所傳送資料內容的大小(byte) result = result + "ContentLength: " + request.ContentLength.ToString() + "\r\n"; if (request.ContentType != null) // 用戶端所傳送資料內容的MIME格式 result = result + "ContentType: " + request.ContentType.ToString() + "\r\n"; if (request.Credentials != null) // 用戶端網路憑證 result = result + "Credentials: " + request.Credentials.ToString() + "\r\n"; result = result + "Headers: " + request.Headers.ToString() + "\r\n"; // 用戶端所傳送資料內容的標題資訊 result = result + "Method: " + request.Method.ToString() + "\r\n"; // 用戶端所使用的通訊協定方法 result = result + "PreAuthenticate: " + request.PreAuthenticate + "\r\n"; // 是否要求預先驗證 result = result + "RequestUri: " + request.RequestUri.ToString() + "\r\n"; // 用戶端所傳送的URI Console.WriteLine(result); } 8 陳鍾誠 - 2018/11/21
WebResponseTest 陳鍾誠 - 2018/11/21 class WebResponseTest { static void Main(String[] args) { System.Net.WebRequest request = WebRequest.Create(new Uri("http://www.google.com/")); ; System.Net.WebResponse response = request.GetResponse(); ; string requestText = ""; requestText = requestText + "ContentLength: " + response.ContentLength.ToString() + "\r\n"; requestText = requestText + "ContentType: " + response.ContentType.ToString() + "\r\n"; requestText = requestText + "ResponseUri: " + response.ResponseUri.ToString() + "\r\n"; System.IO.Stream respStream = response.GetResponseStream(); StreamReader respStreamReader = new StreamReader(respStream, Encoding.Default); string responseText = ""; char[] cbuffer = new char[256]; int byteRead = respStreamReader.Read(cbuffer, 0, 256); while (byteRead != 0) { string strResp = new string(cbuffer, 0, byteRead); responseText = responseText + strResp; byteRead = respStreamReader.Read(cbuffer, 0, 256); } respStream.Close(); response.Close(); Console.WriteLine("Request : \r\n"+requestText); Console.WriteLine("Response : \r\n"+responseText); 9 陳鍾誠 - 2018/11/21