Skip to content

Commit 1950a9c

Browse files
committed
Adding client-level cookie container and use it with the new way to add cookies to the request headers
1 parent 159c8a7 commit 1950a9c

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

src/RestSharp/Options/RestClientOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
136136
/// </summary>
137137
public string? BaseHost { get; set; }
138138

139+
/// <summary>
140+
/// Custom cookie container to be used for requests. RestSharp will not assign the container to the message handler,
141+
/// but will fetch cookies from it and set them on the request.
142+
/// </summary>
143+
public CookieContainer? CookieContainer { get; set; }
144+
139145
/// <summary>
140146
/// Maximum request duration in milliseconds. When the request timeout is specified using <seealso cref="RestRequest.Timeout"/>,
141147
/// the lowest value between the client timeout and request timeout will be used.

src/RestSharp/RestClient.Async.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
106106
// Make sure we have a cookie container if not provided in the request
107107
var cookieContainer = request.CookieContainer ??= new CookieContainer();
108108

109-
var headers = new RequestHeaders();
110-
headers.AddHeaders(request.Parameters);
111-
headers.AddHeaders(DefaultParameters);
112-
headers.AddAcceptHeader(AcceptedContentTypes);
113-
headers.AddCookieHeaders(cookieContainer, url);
109+
if (Options.CookieContainer != null) {
110+
cookieContainer.Add(Options.CookieContainer.GetCookies(url));
111+
}
112+
113+
var headers = new RequestHeaders()
114+
.AddHeaders(request.Parameters)
115+
.AddHeaders(DefaultParameters)
116+
.AddAcceptHeader(AcceptedContentTypes)
117+
.AddCookieHeaders(cookieContainer, url);
114118
message.AddHeaders(headers);
115119

116120
if (request.OnBeforeRequest != null) await request.OnBeforeRequest(message).ConfigureAwait(false);

src/RestSharp/RestClient.Extensions.Params.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
// limitations under the License.
1414
//
1515

16+
using System.Net;
17+
using System.Text;
18+
1619
namespace RestSharp;
1720

1821
public static partial class RestClientExtensions {
1922
/// <summary>
2023
/// Add a parameter to use on every request made with this client instance
2124
/// </summary>
22-
/// <param name="client">RestClient instance</param>
23-
/// <param name="parameter">Parameter to add</param>
25+
/// <param name="client"><see cref="RestClient"/> instance</param>
26+
/// <param name="parameter"><see cref="Parameter"/> to add</param>
2427
/// <returns></returns>
2528
public static IRestClient AddDefaultParameter(this IRestClient client, Parameter parameter) {
2629
client.DefaultParameters.AddParameter(parameter);
@@ -31,7 +34,7 @@ public static IRestClient AddDefaultParameter(this IRestClient client, Parameter
3134
/// Adds a default HTTP parameter (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
3235
/// Used on every request made by this client instance
3336
/// </summary>
34-
/// <param name="client"><see cref="RestClientOptions"/> instance</param>
37+
/// <param name="client"><see cref="RestClient"/> instance</param>
3538
/// <param name="name">Name of the parameter</param>
3639
/// <param name="value">Value of the parameter</param>
3740
/// <returns>This request</returns>
@@ -46,7 +49,7 @@ public static IRestClient AddDefaultParameter(this IRestClient client, string na
4649
/// - RequestBody: Used by AddBody() (not recommended to use directly)
4750
/// Used on every request made by this client instance
4851
/// </summary>
49-
/// <param name="client"><see cref="RestClientOptions"/> instance</param>
52+
/// <param name="client"><see cref="RestClient"/> instance</param>
5053
/// <param name="name">Name of the parameter</param>
5154
/// <param name="value">Value of the parameter</param>
5255
/// <param name="type">The type of parameter to add</param>
@@ -57,7 +60,7 @@ public static IRestClient AddDefaultParameter(this IRestClient client, string na
5760
/// <summary>
5861
/// Adds a default header to the RestClient. Used on every request made by this client instance.
5962
/// </summary>
60-
/// <param name="client"><see cref="RestClientOptions"/> instance</param>
63+
/// <param name="client"><see cref="RestClient"/> instance</param>
6164
/// <param name="name">Name of the header to add</param>
6265
/// <param name="value">Value of the header to add</param>
6366
/// <returns></returns>
@@ -79,7 +82,7 @@ public static IRestClient AddDefaultHeaders(this IRestClient client, Dictionary<
7982
/// <summary>
8083
/// Adds a default URL segment parameter to the RestClient. Used on every request made by this client instance.
8184
/// </summary>
82-
/// <param name="client"><see cref="RestClientOptions"/> instance</param>
85+
/// <param name="client"><see cref="RestClient"/> instance</param>
8386
/// <param name="name">Name of the segment to add</param>
8487
/// <param name="value">Value of the segment to add</param>
8588
/// <returns></returns>
@@ -89,7 +92,7 @@ public static IRestClient AddDefaultUrlSegment(this IRestClient client, string n
8992
/// <summary>
9093
/// Adds a default URL query parameter to the RestClient. Used on every request made by this client instance.
9194
/// </summary>
92-
/// <param name="client"><see cref="RestClientOptions"/> instance</param>
95+
/// <param name="client"><see cref="RestClient"/> instance</param>
9396
/// <param name="name">Name of the query parameter to add</param>
9497
/// <param name="value">Value of the query parameter to add</param>
9598
/// <returns></returns>

0 commit comments

Comments
 (0)