为HttpWebRequest cookie发送逗号值

时间:2014-07-15 11:26:16

标签: c# asp.net cookies

我正在尝试向Asp.net页面发送带有请求的cookie。根据我的理解,它希望cookie值不被编码。如果我对值进行编码,则不会对其进行注册。

CookieContainer不允许我添加非编码值。我好像找不到工作......

我的代码基本上是

HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(@"http:\\whatever");
string cookieName = "whatevername";
string cookieValue = "version=1&Type=a,b,c,d,e,f";
Cookie cook = new Cookie(cookieName, cookieValue, "/", "mypage");
CookieContainer cookies = new CookieContainer();
cookies.Add(cook);
request.CookieContainer = cookies;

这会抛出一个CookieException,说“The 'Value'='version=1&Type=a,b,c,d,e,f' part of the cookie is invalid.

1 个答案:

答案 0 :(得分:0)

您可以尝试UrlEncoding值。

HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(@"http:\\whatever");
string cookieName = "whatevername";
string cookieValue = "version=1&Type=a,b,c,d,e,f";
Cookie cook = new Cookie(cookieName, Server.UrlEncode(cookieValue), "/", "mypage");
CookieContainer cookies = new CookieContainer();
cookies.Add(cook);
request.CookieContainer = cookies;