在.NET 4.5挣扎着试图让饼干出来回应与HttpClient的 [英] Struggling trying to get cookie out of response with HttpClient in .net 4.5

查看:123
本文介绍了在.NET 4.5挣扎着试图让饼干出来回应与HttpClient的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code,成功地运作。我无法弄清楚如何获得的cookie了回应。我的目标是,我希望能够在请求设置cookie并获得饼干了回应。思考?

 专用异步任务<串GT;登录(用户名字符串,字符串密码)
    {
        尝试
        {
            字符串的URL =htt​​p://app.agelessemail.com/account/login/;
            URI地址=新的URI(URL);
            VAR POSTDATA =新的List< KeyValuePair<字符串,字符串>>
                               {
                                   新KeyValuePair<字符串,字符串>(用户名,用户名)
                                   新KeyValuePair<字符串,字符串>(密码,密码)
                               };            HttpContent内容=新FormUrlEn codedContent(POSTDATA);
            VAR cookieJar =新的CookieContainer();
            VAR处理器=新HttpClientHandler
                              {
                                  的CookieContainer = cookieJar,
                                  UseCookies = TRUE,
                                  UseDefaultCredentials = FALSE
                              };            VAR的客户=新的HttpClient(句柄)
                                    {
                                        BaseAddress =地址
                                    };
            HTT presponseMessage响应=等待client.PostAsync(URL,内容);
            response.EnsureSuccessStatus code();
            绳体=等待response.Content.ReadAsStringAsync();
            回体;
        }
        赶上(例外五)
        {
            返回e.ToString();
        }
    }

下面是完整的答案是:

 的Htt presponseMessage响应=等待client.PostAsync(URL,内容);
            response.EnsureSuccessStatus code();            开放的我们的uri =新的URI(UrlBase);
            变种responseCookies = cookieJar.GetCookies(URI);
            的foreach(在responseCookies饼干饼干)
            {
                字符串cookieName = cookie.Name;
                字符串cookieValue = cookie.Value;
            }


解决方案

要Cookie添加到一个请求,以 CookieContainer.Add(URI,饼干)。在提出请求后,cookie的容器会自动从响应所有的饼干进行填充。然后,您可以调用的getCookies()中检索它们。

 的CookieContainer饼干=新的CookieContainer();
HttpClientHandler处理程序=新HttpClientHandler();
handler.CookieContainer =饼干;HttpClient的客户端=新的HttpClient(处理);
HTT presponseMessage响应= client.GetAsync(http://google.com)。结果;开放的我们的uri =新的URI(http://google.com);
IEnumerable的< Cookie和GT; responseCookies = cookies.GetCookies(URI).Cast< Cookie和GT;();
的foreach(在responseCookies饼干饼干)
    Console.WriteLine(cookie.Name +:+ cookie.Value);到Console.ReadLine();

I've got the following code that works successfully. I can't figure out how to get the cookie out of the response. My goal is that I want to be able to set cookies in the request and get cookies out of the response. Thoughts?

    private async Task<string> Login(string username, string password)
    {
        try
        {
            string url = "http://app.agelessemail.com/account/login/";
            Uri address = new Uri(url);
            var postData = new List<KeyValuePair<string, string>>
                               {
                                   new KeyValuePair<string, string>("username", username),
                                   new KeyValuePair<string, string>("password ", password)
                               };

            HttpContent content = new FormUrlEncodedContent(postData);
            var cookieJar = new CookieContainer();
            var handler = new HttpClientHandler
                              {
                                  CookieContainer = cookieJar,
                                  UseCookies = true,
                                  UseDefaultCredentials = false
                              };

            var client = new HttpClient(handler)
                                    {
                                        BaseAddress = address
                                    };


            HttpResponseMessage response = await client.PostAsync(url,content);
            response.EnsureSuccessStatusCode();
            string body = await response.Content.ReadAsStringAsync();
            return body;
        }
        catch (Exception e)
        {
            return e.ToString();
        }
    }

Here is the complete answer:

            HttpResponseMessage response = await client.PostAsync(url,content);
            response.EnsureSuccessStatusCode();

            Uri uri = new Uri(UrlBase);
            var responseCookies = cookieJar.GetCookies(uri);
            foreach (Cookie cookie in responseCookies)
            {
                string cookieName = cookie.Name;
                string cookieValue = cookie.Value;
            }

解决方案

To add cookies to a request, populate the cookie container before the request with CookieContainer.Add(uri, cookie). After the request is made the cookie container will automatically be populated with all the cookies from the response. You can then call GetCookies() to retreive them.

CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;

HttpClient client = new HttpClient(handler);
HttpResponseMessage response = client.GetAsync("http://google.com").Result;

Uri uri = new Uri("http://google.com");
IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();
foreach (Cookie cookie in responseCookies)
    Console.WriteLine(cookie.Name + ": " + cookie.Value);

Console.ReadLine();

这篇关于在.NET 4.5挣扎着试图让饼干出来回应与HttpClient的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆