Home c# How to save cookies from the site in a separate file and...

How to save cookies from the site in a separate file and to use only them?

Author

Date

Category

Cookies are issued on the site on the site, and this is the most autocarization. So, is it possible to somehow save all cookies once and then just read them from the file?
I write via the XNET library, approximate 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";
}

Checking in the code gives out that cookies are, but how to save them all to the file, I do not know. I tried through the usual streamwriter, but I do not know where to turn, because The file gives either Boolean value, or the name of the object / class / method.

Update
I decided myself, only now I did not figure it out how to recreate them in a new passage.
The code may be overloaded unnecessary, so if someone knows the way easier and shorter, I will be grateful:

streamwriter write = new streamwriter (@ "k: \ aaa.txt");
for (int i = 0; i & lt; req.cookies.count; i ++)
{
  Write.Write (Req.Cookies.keys.Elementat (i) + ":" + Req.Cookies.Values.Elementat (I) + Environment.newline);
}

Answer 1, Authority 100%

I personally wrote code to save cook in an XML file using Linq.

Something like this file format:

& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
& lt; Body & gt;
 & lt; cookies_list & gt;
  & lt; cookie user_name = "somename" c_name = "6A64D0796E5C4074CA" C_VALUE = "YES" C_DOMAIN = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05/17/2057 15:41:44" C_secure = "True" / & gt;
  & lt; cookie user_name = "somename" c_name = "2b132c80be5271bcd9a0dddcc2f12c18" c_value = "yes" c_domain = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05/17/2057 15:41:44" C_secure = "True" / & gt;
  & lt; cookie user_name = "somename" c_name = "punter_key" c_value = "a81b639c8f69931daad24de4a8972632" c_domain = ". Marathonsportsbook.com" C_path = "/" C_Expiries = "05/27/2016 15:41:44" C_secure = "True" / & gt ;
  & lt; cookie user_name = "somename" c_name = "jsessionid" c_value = "web2 ~ f8d01b04bde8c9702a1795521e06b218" c_domain = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05.28.2015 15:46:16" C_secure = "True "/ & gt;
  & lt; cookie user_name = "somename" c_name = "afterloginredirectpath" C_VALUE = "& amp; quot; https: //www.marathonsportsbook.com/en/& quot;" c_domain = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05/28/2015 15:46:16" C_secure = "True" / & gt;
 & lt; / cookies_list & gt;
& lt; / body & gt;

Implementation details here:

How to save on JavaScript + selenium and then reuse cookies?


In general, it will be more correct to use the previously created browser profile. And since I wrote above – not to do.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions