HttpClient not storing cookies in CookieContainer

后端 未结 3 2021
暖寄归人
暖寄归人 2020-12-30 00:32

I\'m using VS2010 +.NET 4.0 + System.Net.Http (from Nuget).

For a reason which I don\'t manage to understand, the

相关标签:
3条回答
  • 2020-12-30 00:45

    I guess the problem is that your cookies are secure. The problem is that, CookieContainer won't send secure cookies back to the server in subsequent HTTP requests. It might be a bug, or maybe it has some reasons behind it.

    A workaround is to re-add the cookie to CookieContainer manually. This way, cookie would be sent back in HTTP request header, as no secure would be defined when you send cookies back to the server.

    See this article for more information.

    0 讨论(0)
  • 2020-12-30 00:46

    Use this piece of code to retrieve cookies from response:

    /// <summary>
    /// Read web cookies
    /// </summary>
    public static CookieContainer ReadCookies(this HttpResponseMessage response)
    {
        var pageUri = response.RequestMessage.RequestUri;
    
        var cookieContainer = new CookieContainer();
        IEnumerable<string> cookies;
        if (response.Headers.TryGetValues("set-cookie", out cookies))
        {
            foreach (var c in cookies)
            {
                cookieContainer.SetCookies(pageUri, c);
            }
        }
    
        return cookieContainer;
    }
    
    0 讨论(0)
  • 2020-12-30 00:46

    Perhaps the problem is that your request Url path is to the root of the web site ("/"), but your Set-Cookie header indicates a Path of "/member".

    0 讨论(0)
提交回复
热议问题