Using cookies with HttpClient

August 6, 2013


This simple code snippet shows how to store and use cookies from a web request using the .Net HttpClient. For example, if an API or site sends a token or authentication cookie, you may need to store it and send it on all subsequent requests to show that you are authenticated. This would be for a session-based service. The key is to create a single CookieContainer instance and a single HttpClientHandler instance to hold it. Assume the following class is registered as a singleton.