Marc Rufer bio photo

Marc Rufer

Software engineer
@d-fens GmbH

Twitter Google+ LinkedIn Github Stackoverflow
RSS Feed

HOWTO Set Cookie Header on DefaultRequestHeaders of HttpClient

Marc Rufer 27 Dec 2016 .NET C# HttpClient

For one of our customers we had to implement Cookie handling for authentication purposes. This means reading the session token out of the Set-Cookie header and send the session token in the Cookie header of every request. When using the HttpClient from System.Net.Http there are two possibilites to do that. Either by passing a HttpClientHandler with a CookieContainer to the HttpClient, which then automatically handles cookies (See here), or by manually handle them. As we create a new instance of the HttpClient in our wrapper for every request, we had to go for the 2nd option.

We then tried to manually add the Cookie header with the session token as value to the DefaultRequestHeaders of the HttpClient as follows.

using (var httpClient = new HttpClient())
{
	var uri = new Uri("http://www.example.com");
	httpClient.BaseAddress = uri;
	httpClient.DefaultRequestHeaders.Add("Cookie", "auth=ArbitrarySessionToken");
	var response = httpClient.GetAsync(uri).Result;
}

Adding the Cookie header to the DefaultRequestHeaders worked fine without any exception. However unexpectedly the Cookie header got ignored by the HttpClient and was not present in the request. We searched in the internet for a solution to solve this problem and found the answer on StackOverflow. The solution was to pass a HttpClientHandler with the UseCookies property set to false to the constructor of the HttpClient.

using (var httpClient = new HttpClient(new HttpClientHandler { UseCookies = false }))
{
	var uri = new Uri("http://www.example.com");
	httpClient.BaseAddress = uri;
	httpClient.DefaultRequestHeaders.Add("Cookie", "auth=ArbitrarySessionToken");
	var response = httpClient.GetAsync(uri).Result;
}
comments powered by Disqus

Posts

HOWTO Access Azure File Share in PowerShell Script executed by a Scheduled Task BUG "Task Scheduler service is not available" Error, if specifying Network Connection Condition HOWTO Sync OneDrive on Server even if Windows User not logged in HOWTO Access Microsoft Access Database with PowerShell Get NetworkCredential from PSCredential splits Username into Username and Domain HOWTO Create LocalDB File (.mdf) manually in Visual Studio 2015 HOWTO Set Cookie Header on DefaultRequestHeaders of HttpClient HOWTO Set up PowerShell Remote Session Configuration HOWTO Analyze PowerShell Scripts with PSScriptAnalyzer HOWTO Sign PDF with SuisseID NoBrainer PUT/PATCH/MERGE with OData Service Client Apply Commit from one Repository to another GitHub Repository creation with PowerShell HOWTO Sign PowerShell Script with SuisseID Swissunihockey game schedule PDF generator HOWTO Maven Release on JetBrains TeamCity Limitations when running Activiti in H2 Embedded Mode HOWTO Pylint Integration in IntelliJ 14 HOWTO Install Windows 10 IoT Core on Raspberry PI 2 from a VM First steps with Dropwizard Allman code style for IntelliJ First steps with Flyway IntelliJ - Setup custom inspection profiles Configuration properties meta-data support in IntelliJ AMQP Integration with spring integration OCA Java SE 7 Programmer I Certification Guide Swissunihockey key matcher Swisscom SMS-API-Client Some hints concerning logging with Log4j IFTTT A brilliant service Run deltacloud on a VirtualBox-VM Project specific maven settings in IntelliJ AngularJS Javascript for beginners Liferay in Action