HttpClient的不节能饼干 [英] HttpClient Not Saving Cookies

查看:115
本文介绍了HttpClient的不节能饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用新的HttpClient来处理我的项目的网上冲浪需求;不过,虽然正确设置,HttpClient的不饼干保存到Cookie的容器,它始终是空的。



代码



 私人的CookieContainer _cookieContainer =新的CookieContainer(); 
私人的HttpClient HttpClient的{搞定;组; }
私人HttpClientHandler HttpClientHandler {搞定;组; }

公共初始化()
{
HttpClientHandler =新HttpClientHandler
{
AllowAutoRedirect = TRUE,
UseCookies = TRUE,
=的CookieContainer _cookieContainer
};
HttpClient的=新的HttpClient(HttpClientHandler);
}

公众的CookieContainer饼干
{
{返回_cookieContainer; }
集合{_cookieContainer =价值; }
}

公共无效TEST()
{
//这是总是空的,但我相信,该网站是节省登录的Cookie
VAR饼干饼干=;
}


解决方案

奇怪...你有没有试图直接使用HttpClientHandler的的CookieContainer



代码:

 公开初始化()
{
HttpClientHandler =新HttpClientHandler
{
AllowAutoRedirect = TRUE,
UseCookies = TRUE,
的CookieContainer =新的CookieContainer()
};
HttpClient的=新的HttpClient(HttpClientHandler);
}

公众的CookieContainer饼干
{
{返回HttpClientHandler.CookieContainer; }
集合{HttpClientHandler.CookieContainer =价值; }
}


I am using the new HttpClient to handle my project's web surfing needs; However, although correctly set, the HttpClient does not save the cookies to the Cookie container and it is always EMPTY.

Code

private CookieContainer _cookieContainer = new CookieContainer();
private HttpClient HttpClient { get; set; }
private HttpClientHandler HttpClientHandler { get; set; }

public Initialize()
{
    HttpClientHandler = new HttpClientHandler
                            {
                                AllowAutoRedirect = true,
                                UseCookies = true,
                                CookieContainer = _cookieContainer
                            };
    HttpClient = new HttpClient(HttpClientHandler);
}

public CookieContainer Cookies
{
    get { return _cookieContainer; }
    set { _cookieContainer = value; }
}

public void TEST()
{
    //This is always empty, although I am sure that the site is saving login cookies
    var cookies = Cookies;
}

解决方案

Weird... Did you tried to directly use the HttpClientHandler's CookieContainer ?

Code :

public Initialize()
{
    HttpClientHandler = new HttpClientHandler
                            {
                                AllowAutoRedirect = true,
                                UseCookies = true,
                                CookieContainer = new CookieContainer()
                            };
    HttpClient = new HttpClient(HttpClientHandler);
}

public CookieContainer Cookies
{
    get { return HttpClientHandler.CookieContainer; }
    set { HttpClientHandler.CookieContainer = value; }
}

这篇关于HttpClient的不节能饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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