c#简单代码:用获得的cookie访问目标网站
2020-12-13 16:39
标签:style blog http io color ar os sp for 首先做一个最简单的cookie web application,代码如下。 然后就是写一个Console Application,用获得的Cookie去访问目标网站(假定cookie已获取)。 c#简单代码:用获得的cookie访问目标网站 标签:style blog http io color ar os sp for 原文地址:http://www.cnblogs.com/chunyih/p/4085895.htmlprotected 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);
}
}
}
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);
}