none
请问 system.net.httpclient 怎么处理cookie(request 和response) RRS feed

  • 问题

  • 请问 system.net.httpclient 怎么处理cookie(request 和response)  

    我在response.head里可以看到  set-cookie 但是 request head里没有 cookie


    2014年6月6日 4:21

答案

  • Hi,

    你可以参照以下的代码来做:

    var baseAddress = new Uri("http://example.com");
    var cookieContainer = new CookieContainer();
    using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
    using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
    {
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("foo", "bar"),
            new KeyValuePair<string, string>("baz", "bazinga"),
        });
        cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
        var result = client.PostAsync("/test", content).Result;
        result.EnsureSuccessStatusCode();
    }

    更具体一些的信息请到这里询问http://social.microsoft.com/Forums/zh-CN/home?forum=wcfzhchs 现在的这个论坛不是很合适



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月9日 9:03
    版主