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

  • Question

  • User-760472622 posted

    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 are so many 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: www.testsite.com) 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 http://scooterlabs.com/echo Like this: (C# RestSharp)

    var client = new RestClient("http://scooterlabs.com/echo");
    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? But text browser can't run JavaScript, and I don't think to run JavaScript is necessary to get the cookies. Like the above example, the 2 name value pairs are:

    [{ 'name': 'geoipcountrycode', 'value': 'US' }, { 'name': 'time', 'value': '1579357172779' }];

    Please advice,

    Thanks,

    </div> </div>

    Wednesday, February 5, 2020 11:01 PM

All replies

  • User-760472622 posted

    Hello:

    Thank you very for your code.  I can see your code can get cookies from response of headers, but I can't figure out how to use your code to convert a list of name value pairs to cookies.  If we assume that all cookies have the same domain name, same path(/), same expire time (one month from now).  Just like in my question:

    How I can convert a list of name value pairs of [{ 'name': 'geoipcountrycode', 'value': 'US' }, { 'name': 'time', 'value': '1579357172779' }] into 2 cookies?

    Please share your code for the conversion on top of your code here (if necessary).

    Thanks,

    Thursday, February 6, 2020 4:23 PM
  • User288213138 posted

    Hi zydjohn2019,

    zydjohn2019

    var client = new RestClient("http://scooterlabs.com/echo");

    The RestSharp you use is a three party component, and I suggest posting your question to the relevant forum.

    zydjohn2019

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

    If you want add cookie value for the HttpClient's HttpRequestMessage. you can refer to below code:

    var baseAddress = new Uri("http://example.com");
    var cookieContainer = new CookieContainer();
    using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
    using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
    {  
        cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
    }

    Best regards,

    Sam

    Tuesday, February 11, 2020 7:11 AM