C# 發起HTTP請求并檢查回應的Cookie數據

n342 9年前發布 | 910 次閱讀 C#

 
/**
   Examine Cookies.

To see what cookies a Web Site uses, specify its name on the command line. For example, if you call this program Cookie, then

 Cookie http://MSN.COM 

displays the cookies associated with MSN.COM. */

using System; using System.Net;

public class CookieDemo {
public static void Main(string[] args) {

if(args.Length != 1) { 
  Console.WriteLine("Usage: CookieDemo <uri>"); 
  return ; 
} 

// Create a WebRequest to the specified URI. 
HttpWebRequest req = (HttpWebRequest) 
       WebRequest.Create(args[0]);  

// Get an empty cookie container. 
req.CookieContainer = new CookieContainer(); 

// Send the request and return the response. 
HttpWebResponse resp = (HttpWebResponse) 
       req.GetResponse(); 

// Display the cookies. 
Console.WriteLine("Number of cookies: " +  
                    resp.Cookies.Count); 
Console.WriteLine("{0,-20}{1}", "Name", "Value"); 

for(int i=0; i < resp.Cookies.Count; i++) 
  Console.WriteLine("{0, -20}{1}", 
                     resp.Cookies[i].Name, 
                     resp.Cookies[i].Value); 

// Close the Response.  
resp.Close(); 

} }

</pre>

 本文由用戶 n342 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!