HttpWebRequest: Add Cookie to CookieContainer -> ArgumentException (Parametername: cookie.Domain)

后端 未结 1 2036
野趣味
野趣味 2020-12-14 00:05

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

相关标签:
1条回答
  • 2020-12-14 00:37

    CookieContainers 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" };
    

    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 });
    
    0 讨论(0)
提交回复
热议问题