Upon authorization, cookies are issued on the site, and each time the program is launched, this authentication is performed. So, is it possible to somehow save all the cookies once and then just read them from the file?
I am writing through the xNet library, sample code:

using (var req = new HttpRequest()) { HttpResponse resp; req.Cookies = new CookieDictionary(); req.KeepAlive = true; req.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"; resp = req.Get("http://rbc.ru/"); if (req.Cookies.ContainsKey("uid")) tbInfo.Text = "good"; else tbInfo.Text = "bad"; } 

A check in the code indicates that there are cookies, but I don’t know how to save them all to a file. I tried through the usual StreamWriter, but there I do not know where to go, because in the file gives either a boolean value or the name of the object / class / method.

UPDATE
I decided it myself, but I haven’t figured out how to recreate them in a new room.
The code may be overloaded with unnecessary, so if anyone knows the path easier and shorter, I will be grateful:

 StreamWriter write = new StreamWriter(@"K:\aaa.txt"); for (int i = 0; i < req.Cookies.Count; i++) { write.Write(req.Cookies.Keys.ElementAt(i) + ": " + req.Cookies.Values.ElementAt(i) + Environment.NewLine); } 

    1 answer 1

    I personally wrote code to save the cookies to an XML file using LINQ.

    something like this file format:

     <?xml version="1.0" encoding="utf-8"?> <body> <cookies_list> <cookie user_name="SomeName" c_name="6a64d0796e530a04069945d05c4074ca" c_value="yes" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="17.05.2057 15:41:44" c_secure="True" /> <cookie user_name="SomeName" c_name="2b132c80be5271bcd9a0dddcc2f12c18" c_value="yes" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="17.05.2057 15:41:44" c_secure="True" /> <cookie user_name="SomeName" c_name="PUNTER_KEY" c_value="A81B639C8F69931DAAD24DE4A8972632" c_domain=".marathonsportsbook.com" c_path="/" c_expiries="27.05.2016 15:41:44" c_secure="True" /> <cookie user_name="SomeName" c_name="JSESSIONID" c_value="web2~F8D01B04BDE8C9702A1795521E06B218" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="28.05.2015 15:46:16" c_secure="True" /> <cookie user_name="SomeName" c_name="afterLoginRedirectPath" c_value="&quot;https://www.marathonsportsbook.com/en/&quot;" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="28.05.2015 15:46:16" c_secure="True" /> </cookies_list> </body> 

    implementation details here:

    How to save javascript + selenium and then reuse cookies?


    In general, it would be more appropriate to use a previously created browser profile. And as I wrote above - do not.