HttpRequestHeader cookie格式

时间:2012-09-21 19:33:31

标签: c# .net http web

需要指定HttpRequestHeader.Cookies的格式? F.e,如果我想添加名为CITY的值为NY的cookie,我应该如何使用WebClient.Headers.Add()方法?

3 个答案:

答案 0 :(得分:14)

试试这个样本

  WebClient wb = new WebClient(); 

    wb.Headers.Add(HttpRequestHeader.Cookie, "CITY=NY"); 

许多饼干:

wb.Headers.Add(HttpRequestHeader.Cookie, "cookiename1=cookievalue1; cookiename2=cookievalue2"); 

答案 1 :(得分:3)

要添加Cookie,最好也最容易使用Response.Cookies.Add();

HttpCookie myCookie = new HttpCookie("lastVisit");
myCookie.Value = DateTime.Now.ToString();
myCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(myCookie);

答案 2 :(得分:1)

Headers.Add

 myWebHeaderCollection.Add("CITY","NY");

以下是Cookie标头在最后rfc 6265处的外观:

Cookie: CITY=NY;