Why Response.Cookies is empty?
Question: When I run this python script

    
    
    import requests
    
    main_page_request = requests.get("http://carkit.kg/")
    cookie = main_page_request.cookies.get("csrftoken", "")
    

I'm getting proper result, but when I run this code at C#:

    
    
    string url = @"http://carkit.kg";
    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
    Debug.Log(response.Cookies["csrftoken"]); // prints "Null"
    

it says that response.Cookies is empty. What is the problem?


Answer: You have to add a cookie container to the request. Then it returns the cookie:

    
    
    CookieContainer c = new CookieContainer();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.CookieContainer = c;
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
    Console.WriteLine(response.Cookies["csrftoken"]);
    // prints "csrftoken=E1iRIi7cQvxvJcnSgOgaEP3XPxTHRUfT"
    

