C# HttpWebRequest: 添加 Cookie 到 CookieContainer -> ArgumentException (Parametername: cookie.Domain)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18667931/
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
HttpWebRequest: Add Cookie to CookieContainer -> ArgumentException (Parametername: cookie.Domain)
提问by TorbenJ
I'm trying to login to a website via my application. What I did:
我正在尝试通过我的应用程序登录网站。我做了什么:
First I figured out how the browser does the authorization process with Fiddler. I examined how the POST request is built and I tried to reconstruct it. The browser sends 4 cookies (Google Analytics) and I tried to set them:
首先,我想出了浏览器如何使用 Fiddler 进行授权过程。我检查了 POST 请求是如何构建的,并尝试重建它。浏览器发送 4 个 cookie(谷歌分析),我尝试设置它们:
CookieContainer gaCookies = new CookieContainer();
gaCookies.Add(new Cookie("__utma", "#########.###########.##########.##########.##########.#"));
gaCookies.Add(new Cookie("__utmb", "#########.#.##.##########"));
gaCookies.Add(new Cookie("__utmc", "#########"));
gaCookies.Add(new Cookie("__utmz", "#########.##########.#.#.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"));
(just replaced the original cookie data with #)
(只是用#替换了原来的cookie数据)
Then I went through the code with the debugger and as soon as the first gaCookies.Add is executed, the application stops with an
然后我用调试器检查了代码,一旦第一个 gaCookies.Add 被执行,应用程序就会停止
System.ArgumentException: The parameter '{0}' cannot be an empty string. Parameter name: cookie.Domain
I would like to know why this happens. The constructor of Cookie doesn't require a domain and I don't know where I can get this value?
我想知道为什么会发生这种情况。Cookie 的构造函数不需要域,我不知道从哪里可以获得这个值?
Would be very great if someone of you could help me with this.
I'm not a webdeveloper or an expert in web stuff so I don't know much about it.
Is there maybe a great source where I can learn about this if there is no "short and quick answer"?
如果你们中有人能帮我解决这个问题,那就太好了。
我不是网络开发人员或网络方面的专家,所以我对此知之甚少。
如果没有“简短而快速的答案”,是否有一个很好的来源可以让我了解这一点?
采纳答案by Ichabod Clay
CookieContainer
s can hold multiple cookies for different websites, therefor a label (the Domain) has to be provided to bind each cookie to each website. The Domain can be set when instantiating the individual cookies like so:
CookieContainer
s 可以为不同的网站保存多个 cookie,因此必须提供一个标签(域)来将每个 cookie 绑定到每个网站。可以在实例化单个 cookie 时设置域,如下所示:
Cookie chocolateChip = new Cookie("CookieName", "CookieValue") { Domain = "DomainName" };
An easy way to grab the domain to is to make a Uri
(if you aren't using one already) that contains your target url and set the cookie's domain using the Uri.Host
property.
获取域的一种简单方法是创建一个Uri
(如果您还没有使用)包含您的目标 url 并使用该Uri.Host
属性设置 cookie 的域。
CookieContainer gaCookies = new CookieContainer();
Uri target = new Uri("http://www.google.com/");
gaCookies.Add(new Cookie("__utmc", "#########") { Domain = target.Host });