RestSharp 109: 在每个请求中添加cookies

huangapple go评论65阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年3月21日 02:46:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794165-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定