Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookies are not added to cookie container when running on iOS #2168

Open
h-arshad opened this issue Feb 2, 2024 · 2 comments
Open

Cookies are not added to cookie container when running on iOS #2168

h-arshad opened this issue Feb 2, 2024 · 2 comments
Labels

Comments

@h-arshad
Copy link

h-arshad commented Feb 2, 2024

When using rest sharp in NET MAUI application development, for the same API call that is supposed to inject a cookie, in Android you will see the cookie but it is missing when running in iOS device.

Code Sample:

async Task TestCookiesAsync(){
var restClient = new RestSharp.RestClient(new RestSharp.RestClientOptions()
{
  BaseUrl = new Uri("https://google.com"),
  CookieContainer = new System.Net.CookieContainer()
});
var request = new RestSharp.RestRequest("", RestSharp.Method.Get);
var response = await restClient.ExecuteAsync(request);
Console.WriteLine(response.Cookies.Count);
}

Expected behaviour:
The response cookie should be added inside the cookie container.

  • OS: iOS 17.2.1 (iPhone 11 )
  • NET MAUI .NET 8
  • Rest Sharp V 110.2.0
@h-arshad h-arshad added the bug label Feb 2, 2024
@rassilon
Copy link

Based on what I see at https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.InvokeNativeHandler.cs

This probably has something to do with the differences between the native handlers on iOS vs Android.
i.e. Xamarin.Android.Net.AndroidMessageHandler, Mono.Android vs System.Net.Http.NSUrlSessionHandler, Microsoft.iOS

If a redirect is involved, it might be related to #2059, although I wonder why the behavior would differ between mobile platforms. :(

@h-arshad
Copy link
Author

Thank you for your response and looking into the post.
In fact I do not suspect the issue comes from those handlers because eventually due to the cookie issue I had with Restsharp, I replaced it with the native HttpClient and it works fine on both platforms.
I put an example of the working code below.

private static readonly System.Net.Http.HttpClientHandler HttpClientMessageHandler = new()
 {
     AllowAutoRedirect = false,
     UseCookies = true,
     CookieContainer = new(),
 };
 private static readonly System.Net.Http.HttpClient RestClient = new(HttpClientMessageHandler)
 {
     BaseAddress = new("https://google.com"),
     Timeout = TimeSpan.FromSeconds(60),
     DefaultRequestHeaders =
     {
         { "User-Agent", "mobile"},
         { "Accept", "application/json" }
     }
 };

The one can simply use var response = await RestClient.GetAsync();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants