c#简单代码:用获得的cookie访问目标网站

2020-12-13 16:39

阅读:267

标签:style   blog   http   io   color   ar   os   sp   for   

首先做一个最简单的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);
}

 

c#简单代码:用获得的cookie访问目标网站

标签:style   blog   http   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/chunyih/p/4085895.html


评论


亲,登录后才可以留言!