HttpWebRequest的:添加cookie来的CookieContainer - > ArgumentException的(参数名称:cookie.Domain) [英] HttpWebRequest: Add Cookie to CookieContainer -> ArgumentException (Parametername: cookie.Domain)
问题描述
我想通过我的应用程序登录到网站。
我做了什么:
I'm trying to login to a website via my application. What I did:
首先,我想通了浏览器如何与小提琴手授权过程。
我检查了POST请求是如何构建的,我试图重建它。
中的浏览器发送4饼干(谷歌分析),我试图让它们:
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:
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)"));
(刚刚替换#原来的cookie数据)
(just replaced the original cookie data with #)
然后,我通过与调试代码去了,只要第一gaCookies.Add执行,应用程序与
Then I went through the code with the debugger and as soon as the first gaCookies.Add is executed, the application stops with an
System.ArgumentException: The parameter '{0}' cannot be an empty string. Parameter name: cookie.Domain
我想知道为什么会这样。 Cookie的构造函数不需要域名,我不知道在哪里可以得到这个值?
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?
将是非常巨大的,如果你有人可以帮助我这一点。结果
我不是一个webdeveloper或网页内容方面的专家,所以我不很了解。结果
也许有一个伟大的来源在那里我可以了解这个如果没有短,快回答?结果
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"?
推荐答案
的CookieContainer
s时,可以容纳多个Cookie为不同的网站,为此标签(域)必须提供给每个cookie来每个网站绑定。域可以实例化具体Cookie像时可以设置这样:
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:
Cookie chocolateChip = new Cookie("CookieName", "CookieValue") { Domain = "DomainName" };
一个简单的方法抢域名就是让乌里
(如果你不使用的话)包含您的目标URL,并设置使用 Uri.Host
属性中的cookie的域。
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.
CookieContainer gaCookies = new CookieContainer();
Uri target = new Uri("http://www.google.com/");
gaCookies.Add(new Cookie("__utmc", "#########") { Domain = target.Host });
这篇关于HttpWebRequest的:添加cookie来的CookieContainer - > ArgumentException的(参数名称:cookie.Domain)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!