c#简单代码:用获得的cookie访问目标网站
首先做一个最简单的cookie web application,代码如下。
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { HttpCookie cookie = Request.Cookies["MyCookieName"]; if (cookie != null) { Response.Write("cookie received"); } else { HttpCookie newCookie = new HttpCookie("MyCookieName", "test"); newCookie.Expires = new DateTime(2015, 1, 1); Response.Cookies.Add(newCookie); } } }
然后就是写一个Console Application,用获得的Cookie去访问目标网站(假定cookie已获取)。
using (WebClient client = new WebClient()) { string cookieStr = string.Format("MyCookieName=test"); client.Headers["Cookie"] = cookieStr; string retStr = client.DownloadString("http://www.yoursite.com"); Console.WriteLine(retStr); }