将标头cookie字符串映射到CookieCollection,反之亦然 [英] Mapping Header cookie string to CookieCollection and vice versa
问题描述
考虑使用此标头的网络响应:
Consider a web response with this header:
Set-Cookie: sample=testCookie; Domain=.sample.com; Expires=Tue, 25-Jan-2012 00:49:29 GMT; Path=/
此标头将映射到 CookieCollection
在 .NET
中。而且当我们处理 CookieCollection
时,它最终将转换为这样的标题字符串
。
this header would be mapped to CookieCollection
in .NET
. And also when we deal with a CookieCollection
it will finally converted to such a header string
.
我正在寻找某种方式纯粹以两种方式进行转换。肯定 .NET
在它的内部库中有它。我相信任何从文本构造对象模型的类应该支持两种方法(这里 CookieCollection
):
I'm looking some way to purely do this conversions in two way. Surely .NET
has it in it's internal library. I believe any class which constructs object model from text and vice versa should support two methods (here CookieCollection
):
// Creating cookie collection from header text
CookieCollection.TryParse(cookieHeaderString, out myCookieCollection);
// and getting the final header which would be sent by request
String cookieHeaderString = myCookieCollection.GetCookieHeaderString();
如何使用 CookieCollection
实现这一目标?
How can I achieve that with CookieCollection
?
推荐答案
我认为你在寻找的CookieContainer 。请参阅SetCookies方法。
I think you are looking for CookieContainer. See SetCookies method.
这篇关于将标头cookie字符串映射到CookieCollection,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!