使用 .net (C#) System.Net.Cookie 处理 cookie 值中的逗号
我正在创建一个客户端来访问网站并登录并自动执行一些任务,但是他们最近更新了他们的 cookie(无论出于何种原因......)在其标识 cookie 中包含一个逗号。
例如,Cookie 将具有类似于以下的值:
a,bcdefghijklmnop
问题是,根据 msdn 您不能在 cookie 值中使用逗号或句点。 我正在寻找一种方法来解决这个限制,一些方法可以使.net的Cookie与逗号很好地配合。 我发现服务器确实向客户端发送了一个“SET-COOKIE”标头,我猜这就是正在解析的内容,但这似乎也显然给命令和分号赋予了特殊含义(因此限制.NET 本身内部的类)。
但是,IE、Firefox 等浏览器如何正确处理 cookie(正如它们显然所做的那样,因为该网站在我测试过的任何浏览器中都运行良好。)是否有一种方法可以强制执行此操作.NET 中的行为?
任何帮助将不胜感激,谢谢。
--- 编辑 ---
一些附加信息:
我的代码看起来像这样:
request = (HttpWebRequest)WebRequest.Create(URI);
request.CookieContainer = Program.client.cookieJar;
cookieJar 在 Program.client 中定义为:
CookieContainer cookieJar = new CookieContainer();
当我循环并打印 CookieContainer 中的所有 cookie 时,我得到类似这样的内容: (cookies ,格式为:“name”->“value”)
"normal_cookie" -> "i am the value"
"messedup_cookie" -> "a"
"bcdefghijklmnop" -> ""
// What I should get is this:
"normal_cookie" -> "i am the value"
"messedup_cookie" -> "a,bcdefghijklmnop"
问题的核心似乎是逗号和分号是 SET-COOKIE 标头字符串中的保留字符...但是浏览器如何处理这个问题呢? 我可能可以自己解析该字符串,但我不知道如何解决诸如此类的情况:(HTTP 标头,格式为:“名称”->“值”)
"Set-Cookie" -> "messedup_cookie=a,bcdefghijklmnop; path=/; domain=.domain.com; expires=Sat, 15-Aug-2009 09:14:24 GMT,anothervariable=i am the value;"
如您所见,过期部分看起来使用逗号而不是分号来将其与下一个变量区分开来。 据我所知,它的格式是:
cookie1_var1=value; cookie1_var2=value,cookie2_var1=value; cookir2_var2=value
但如果这是真的,是否有一种优雅的方法来处理其中一个值中可能出现的逗号?
I'm creating a client to visit a website and log in + do some tasks automatically, however they recently updated their cookies to (for whatever reason...) contain a comma inside their identification cookie.
So for example, the Cookie will have a value similar to this:
a,bcdefghijklmnop
The problem is, according to msdn you can't use a comma nor a period inside a cookie's value. What I'm looking for is a way around this limitation, some way to make .net's Cookie's work nice with the commas. I've found that the server does send a 'SET-COOKIE' header to the client and I'm guessing that's what is being parsed, but that also seems to obviously give special meaning to commans and semicolons as well (thus the limitation of the class inside .NET itself).
But then how does a browser such as IE, Firefox, etc... handle the cookie properly (as they clearly do, since the website works fine in any browsers I've tested it with.) Is there maybe a way to force this behaviour in .NET?
Any help would be appreciated, thanks.
--- EDIT ---
some additional information:
My code looks something like this:
request = (HttpWebRequest)WebRequest.Create(URI);
request.CookieContainer = Program.client.cookieJar;
Where cookieJar is defined in Program.client as:
CookieContainer cookieJar = new CookieContainer();
When i loop through and print out all the cookies in the CookieContainer, I get something like this: (cookies, in the format: "name" -> "value")
"normal_cookie" -> "i am the value"
"messedup_cookie" -> "a"
"bcdefghijklmnop" -> ""
// What I should get is this:
"normal_cookie" -> "i am the value"
"messedup_cookie" -> "a,bcdefghijklmnop"
The core of the problem seems to be that commas and semi colons are reserved characters in the SET-COOKIE header string...but then how do browsers handle this? I can probably parse the string myself but I don't know how to get around situations such as this one: (HTTP header, in the format: "name" -> "value")
"Set-Cookie" -> "messedup_cookie=a,bcdefghijklmnop; path=/; domain=.domain.com; expires=Sat, 15-Aug-2009 09:14:24 GMT,anothervariable=i am the value;"
As you can see, the expires section looks for a comma instead of a semicolon to differentiate itself from the next variable. As far as I can tell, it's in the format:
cookie1_var1=value; cookie1_var2=value,cookie2_var1=value; cookir2_var2=value
But if that's true, is there an elegant way to deal with commas that may occur inside one of the values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 以下内容文章中,您应该考虑使用 UrlEncode 和 UrlDecode 在 cookie 中存储值。
MSDN 也有一篇关于该主题的文章。
According to the following article, you should consider UrlEncode and UrlDecode for storing values in cookies.
MSDN also has an article on the subject.
为了处理 cookie,由于 cookie 本质上是一个字符串,因此您可能希望在设置 cookie 值之前尝试对 cookie 值进行 URL 编码,然后在提取该值时对其进行解码。
即:
类似地:
To handle the cookie, since a cookie is essentially a string, you may like to try URL Encoding the cookie value before setting it, and then Decoding it when you pull out the value.
i.e.:
and similarly: