How to add Cookies in Http Client headers from a list of name value pairs RRS feed

  • Question

  • Hello:

    I have to use HTTP client to post some x-www-form-urlencoded data to a web server, if I do this by hand via web browser, it always works, but when I tried to use HTTP client to do the same thing, I always found more than half of the cookies are missing. For example, I can see from MS Edge developer tools, the cookies have about 30 cookies, they all have the same domain name, same path (/) and 25 of them have the same expire time; only 5 of them are session specific, which I can get those session specific cookies by a text browser; for the other 25 cookies, I can see their names are always the same, the expire time is about one month from today.

    I understand why there aremany cookies from web browser, because, the web site runs quite a number of JavaScript (about 70 js files in total), and each of the JavaScript could set different cookies.

    In order to use HTTP client to post the data to the web server, I think may be I can use C# to generate those 25 cookies from a list of name value pair, and set the same domain (like: and the same path (/) and the same expire time (one month from now).

    I can use some C# code to test some custom headers against

    Like this: (C# RestSharp)

    var client = new RestClient("");
    var request = new RestRequest(Method.GET);
    request.AddHeader("geoipcountrycode", "US");
    request.AddHeader("time", "1579357172779");
    IRestResponse response = client.Execute(request);

    But how I can add a list of name value pairs as cookies, and set cookies to HTTP client, so I can use HTTP client to post data? Or how I can convert a list of name value pairs to a list of same domain, same path, same expire cookies and add to the other 5 session-specific cookies, which I can get from a text browser?

    Like the above example, the 2 name value pairs are:

    [{ 'name': 'geoipcountrycode', 'value': 'US' },

    { 'name': 'time', 'value': '1579357172779' }];

    Please advice,


    Monday, February 3, 2020 7:29 PM

All replies

  • Hi zydjohn,
    I viewed your description, it is recommended to go to ASP.NET forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, February 4, 2020 8:33 AM
  • You aren't using HttpClient which is what we can help you with. You are using Restclient which is a third party component. You'll need to post your question in their forums.

    For HttpClient you would add the cookie as part of client creation. Then you can add cookies as needed. Here's some boilerplate code that is generally used.

    //Do this the very first time you create the HttpClient, do not recreate HttpClient each time!!!
    var cookies = new CookieContainer();
    var handler = new HttpClientHandler()
       CookieContainer = cookies
    var client = new HttpClient(handler);
    //Later add a cookie, note that the cookie container is shared so you only add it if it isn't already there
    cookies.Add(url, new Cookie("myCookie", "value"));

    Note that in your code example you're adding HTTP headers to a request. This isn't the same thing as adding a cookie to the request. Don't mix the 2 as it won't work well.

    However for RestClient you'll need to post in their forums.

    Michael Taylor

    Tuesday, February 4, 2020 2:53 PM