HttpClient PCL Cookies Not Being Applied to Requests

筅森魡賤 提交于 2019-12-10 16:55:42

问题


I'm using .NET HttpClient for PCL (2.2.15) for a common library across Windows Phone 8, Windows 8, and .NET 4.5. However, I don't see cookies being applied to requests.

I'm setting up the HttpClient and HttpClientHandler using the following code. HttpClient is a property on the wrapping class.

CookieContainer = new CookieContainer();

var handler = new HttpClientHandler
{
    AllowAutoRedirect = true,
    CookieContainer = CookieContainer,
    Credentials = client.CredentialCache,
    UseCookies = true,
    AutomaticDecompression = 
          DecompressionMethods.Deflate | DecompressionMethods.GZip
};

HttpClient = new HttpClient(handler, false);

Requests are being sent using the following.

var response = await HttpClient.SendAsync(httpRequestMessage);

I'm able to peek into the CookieContainer on the handler when stepping through in Debug and am able to see the CookieContainer for the domain I'm working against with the expected cookie.

The cookie has the following values:

  • Path = '/'
  • Domain = '.staging3.api.com'
  • Value = [authentication_id]
  • Name = 'API_AuthId'

The subsequent request is roughly: https://staging3.api.com/api/v3/Items

I've also tried setting the cookie myself, however CookieContainer.GetCookieHeader(requestUri) returns an empty string.

Additional information:

If I modify the requestUri to https://www.staging3.api.com/api/v3/Items I'm able to get the cookie header from CookieContainer.GetCookieHeader(requestUri)

来源:https://stackoverflow.com/questions/19819943/httpclient-pcl-cookies-not-being-applied-to-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!