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

Basic stuff: HttpClient not working in WebAssembly App #18912

Closed
hbraasch opened this issue Nov 25, 2024 · 4 comments
Closed

Basic stuff: HttpClient not working in WebAssembly App #18912

hbraasch opened this issue Nov 25, 2024 · 4 comments

Comments

@hbraasch
Copy link

Current behavior

I'm a newbie to Uno, thinking to migrate away from .net MAUI. But I quickly ran into this basic problem:

Using a VERY simple app, you click a button and then access www.google.com using HttpClient. It works when compiled for Android, it fails when compiled for WebAssembly.

The sample repository is here:
https://github.com/hbraasch/UnoHttpClientTester.git

The HttpClient code used is here:

private async void MyButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    try
    {
        using (var client = new HttpClient())
        {
            HttpResponseMessage response = await client.GetAsync("https://www.google.com");
            if (response.IsSuccessStatusCode)
            {
                string responseData = await response.Content.ReadAsStringAsync();
                statusBox.Text = string.IsNullOrEmpty(responseData)? "No data back": "Got data back";
            }
            else
            {
                statusBox.Text = "Error: " + response.StatusCode;
            }
        }
    }
    catch (Exception ex)
    {
        statusBox.Text = ex.Message;
    }
}

This is the result when running Android:

image

This is the result when running WebAssembly:

image

Anu idea what can be wrong?

Help shall be much appreciated.

Expected behavior

The WebAssembly App must respond exactly the same as Android App

How to reproduce it (as minimally and precisely as possible)

Minimal repro project supplied:

https://github.com/hbraasch/UnoHttpClientTester.git

Workaround

Not known

Works on UWP/WinUI

Yes

Environment

No response

NuGet package version(s)

Nothing extra used

Affected platforms

WebAssembly

IDE

Visual Studio 2022

IDE version

N/A

Relevant plugins

N/A

Anything else we need to know?

Have no idea how to fix this

@hbraasch hbraasch added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Nov 25, 2024
@MartinZikmund
Copy link
Member

It may likely be a CORS issue (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing), HttpClient requests are bound by security considerations of the browser, so if the target (in this case Google) does not have permissive CORS, you will not be able to make such API request. This is easy to solve for APIs you control (you can just adjust CORS so that your WASM app's URI is allowed), or many APIs (including Google APIs) allow you to register your app and fill out your app's URI so that you can access them.

@MartinZikmund MartinZikmund added triage/needs-information Indicates an issue needs more information in order to work on it. and removed kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Nov 25, 2024
@hbraasch
Copy link
Author

Thank you! You're right.
I've now added CORS to "a server I control", and it now works fine.

I've just came across a curiosity, if I instantiate a HttpClient in the following way (with a handler that accepts all certificates), it fails with an [Operation not supported on this platform] when doing client.Get() or client.Post(). It still works for Android and Desktop.

Here is the code that exposes it:

            HttpClientHandler handler = new HttpClientHandler();

            //not sure about this one, but I think it should work to allow all certificates:
            handler.ServerCertificateCustomValidationCallback += (sender, cert, chaun, ssPolicyError) =>
            {
                return true;
            };

            var client = new HttpClient(handler, false);

Do you think its an intentional feature, or should I raise it as an issue?

Regards

@github-actions github-actions bot removed the triage/needs-information Indicates an issue needs more information in order to work on it. label Nov 25, 2024
@jeromelaban
Copy link
Member

I've just came across a curiosity, if I instantiate a HttpClient in the following way (with a handler that accepts all certificates), it fails with an [Operation not supported on this platform] when doing client.Get() or client.Post(). It still works for Android and Desktop.

This an intentional feature, yes, coming from the .NET runtime. The underlying fetch API cannot provide that feature, and it's been disabled in the BCL.

@hbraasch
Copy link
Author

We can close this

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

No branches or pull requests

3 participants