locked
Setting Cookies in HttpClient RRS feed

  • Question

  • What is the correct way to set a cookie and read a cookie while using HttpClient to download a file? 
    Thursday, August 15, 2013 5:43 PM

Answers

  • You will need to use the HttpClientHandler instance to manage the cookies for you. For example, you can use the following code:

                Uri uri = new Uri("http://www.microsoft.com");
                HttpClientHandler handler = new HttpClientHandler();
                handler.CookieContainer = new CookieContainer();

                handler.CookieContainer.Add(uri, new Cookie("name", "value")); // Adding a Cookie
                HttpClient client = new HttpClient(handler);
                HttpResponseMessage response = await client.GetAsync(uri);
                CookieCollection collection = handler.CookieContainer.GetCookies(uri); // Retrieving a Cookie


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    • Marked as answer by Knewron Saturday, August 17, 2013 11:32 AM
    Thursday, August 15, 2013 9:01 PM
    Moderator
  • Even better: use the new Windows.Web.Http HTTP objects:

    var bpf = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();

    var cm = bpf.CookieManager;

    var cookie = new HttpCookie("name", ".example.com", "/") { Value = "value" };

    cm.SetCookie(cookie);

    var http = new HttpClient(bpf);

    await http.PostAsync(new Uri("http://example.com/"), new HttpStringContent("content"));

    (note: code compiles, but was not tested)


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Knewron Saturday, August 17, 2013 11:32 AM
    Thursday, August 15, 2013 9:57 PM

All replies

  • You will need to use the HttpClientHandler instance to manage the cookies for you. For example, you can use the following code:

                Uri uri = new Uri("http://www.microsoft.com");
                HttpClientHandler handler = new HttpClientHandler();
                handler.CookieContainer = new CookieContainer();

                handler.CookieContainer.Add(uri, new Cookie("name", "value")); // Adding a Cookie
                HttpClient client = new HttpClient(handler);
                HttpResponseMessage response = await client.GetAsync(uri);
                CookieCollection collection = handler.CookieContainer.GetCookies(uri); // Retrieving a Cookie


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    • Marked as answer by Knewron Saturday, August 17, 2013 11:32 AM
    Thursday, August 15, 2013 9:01 PM
    Moderator
  • Even better: use the new Windows.Web.Http HTTP objects:

    var bpf = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();

    var cm = bpf.CookieManager;

    var cookie = new HttpCookie("name", ".example.com", "/") { Value = "value" };

    cm.SetCookie(cookie);

    var http = new HttpClient(bpf);

    await http.PostAsync(new Uri("http://example.com/"), new HttpStringContent("content"));

    (note: code compiles, but was not tested)


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Knewron Saturday, August 17, 2013 11:32 AM
    Thursday, August 15, 2013 9:57 PM