C# 如何将 CookieCollection 插入 CookieContainer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/546369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert CookieCollection to CookieContainer?
提问by Skuta
After I get response from httpwebrequest, I'd like the cookies obtained to save for the purpose of using them in another httbwebrequest. However, I'd need to insert CookieCollection to CookieContainer. How do I do that? Tried to do:
从 httpwebrequest 得到响应后,我希望将获得的 cookie 保存起来,以便在另一个 httbwebrequest 中使用它们。但是,我需要将 CookieCollection 插入到 CookieContainer。我怎么做?试图做:
request.Cookiecontainer.add(response.Cookies);
but this keeps getting out of error: Object reference not set to an instance of an object.
但这不断出现错误:对象引用未设置为对象的实例。
采纳答案by Todd Friedlich
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(response.Cookies);
According to Microsoft:
根据微软的说法:
CookieContainer is a null reference (Nothing in Visual Basic) by default. You must assign a CookieContainer object to the property to have cookies returned in the Cookies property of the HttpWebResponse returned by the GetResponse method.
默认情况下,CookieContainer 是一个空引用(在 Visual Basic 中为 Nothing)。您必须将 CookieContainer 对象分配给该属性,以便在 GetResponse 方法返回的 HttpWebResponse 的 Cookies 属性中返回 cookie。
回答by John Rasch
request.CookieContainer.Add(response.Cookies);
回答by Todd Friedlich
dim cookie as new cookiecontainer
//request codes here
//response here
cookie.add(response.cookies)