WebClient中如何初始化cookie

How to initialize cookie in WebClient

this 问题的投票最多的答案中有这一行:

cookieJar.Add(new Cookie("my_cookie", "cookie_value", "/", "mysite"));

在这一行中,有"my_cookie"、cookie_value和"mysite"字段。我不知道如何填写这些行。你能告诉我如何用例子来填充那些吗?提前致谢。

希望对您有所帮助

CookieContainer gaCookies = new CookieContainer();
Uri target = new Uri("http://www.google.com/");    
gaCookies.Add(new Cookie("__utmc", "#########") { Domain = target.Host });

或前往 here 了解更多信息

试试这个:

HttpCookie _cookie = new HttpCookie("Department"); // Create and give name
_cookie.Expires = DateTime.Now.AddDays(30);       // expries in one month
_cookie.Value = "Dep1";                          // set value
HttpContext.Response.Cookies.Add(_cookie);      // add cookie to the context