如何插入CookieCollection到的CookieContainer? [英] How to insert CookieCollection to CookieContainer?
问题描述
在我从HttpWebRequest的回应,我想获得保存在另一个httbwebrequest使用它们的目的的cookie。不过,我需要插入到CookieCollection的CookieContainer。我怎么做?试图这样做:
request.Cookiecontainer.add(response.Cookies);
但这样下去失控的错误:未将对象引用设置到对象的实例
request.CookieContainer =新的CookieContainer();
request.CookieContainer.Add(response.Cookies);
据微软:
的CookieContainer为空引用(在Visual Basic中为Nothing)默认情况下。你必须一的CookieContainer对象赋给属性有由的GetResponse方法返回的HttpWebResponse的饼干属性返回饼干。
块引用>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:
request.Cookiecontainer.add(response.Cookies);
but this keeps getting out of error: Object reference not set to an instance of an object.
解决方案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.
这篇关于如何插入CookieCollection到的CookieContainer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!