英文:
RestSharp 109: Adding cookies to every request
问题
在RestSharp 108及以下版本中,RestClient可以像这样为每个调用添加通用的cookie:
RestClient.CookieContainer.Add(new Cookie("hello", "hello", "/", _baseUrl.Host));
但在RestSharp 109中,情况不再如此。我们如何为每个请求添加一系列的cookie?
英文:
In RestSharp 108 and below the RestClient could add common cookies to every call like this:
RestClient.CookieContainer.Add(new Cookie("hello", "hello", "/", _baseUrl.Host));
This is no longer the case in RestSharp 109. How can we add a seriers of cookies to every Request?
答案1
得分: 1
客户端级别的Cookie容器已被移除,因为它在大多数情况下都是有害的,它会在请求之间保留Cookie,这可能导致私人Cookie的意外泄漏。
您仍然可以通过配置消息处理程序向客户端添加自定义Cookie容器:
options.ConfigureMessageHandler =
h => {
var handler = (HttpClientHandler)h;
handler.CookieContainer = myContainer;
handler.UseCookies = true;
return handler;
}
下一个 RestSharp 版本将允许使用通过选项提供的Cookie容器添加默认Cookie(https://github.com/restsharp/RestSharp/pull/2042)
英文:
The client-level cookie container was removed because it was harmful in most of the use cases as it kept cookies between the requests, which might cause undesired leaks of private cookies.
You can still add a custom cookie container to the client by configuring the message handler:
options.ConfigureMessageHandler =
h => {
var handler = (HttpClientHandler)h;
handler.CookieContainer = myContainer;
handler.UseCookies = true;
return handler;
}
The next RestSharp version will allow adding default cookies using the cookie container provided via options (https://github.com/restsharp/RestSharp/pull/2042)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论