Uno: Getting HttpClient to work with Wasm

Created on 15 Jun 2018  路  12Comments  路  Source: unoplatform/uno

I think there's a common issue with mono wasm and HttpClient. I get a ERROR: Operation is not supported on this platform.

Blazer was able to get it working but its not a normal HttpClient. HttpClient doesnt work with Ooui yet either. Is there a work around that Ive missed?

Most helpful comment

I can't seem to get this to work at all.

Using the latest uno template I edit the simple mainpage with a button and label like so:

    public MainPage()
    {
        this.InitializeComponent();
        btn.Click += OnClicked;
    }

    private async void OnClicked(object sender, RoutedEventArgs e)
    {
        lbl.Text = "clicked";
        try
        {
            var wc = new System.Net.Http.HttpClient(new Uno.UI.Wasm.WasmHttpHandler());
            //var client = new System.Net.WebClient();
            var response = await wc.GetStringAsync(new Uri("http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html"));
            lbl.Text = response; 
        }
        catch(Exception ex)
        {
            lbl.Text = ex.Message;
        }
    }

I can't get any variation (including the above) of this to work at all for wasm. Am I missing something?

Never mind - I realize what happened. This works with Edge - not with the new chromium edge though (yet). Sorry.

All 12 comments

@davidlsharp1 - have you seen this closed thread? I haven't tried this but it seems like this is working workaround.

@zbigniew-gajewski thanks, I'll check it out.

So does this mean that I should be able to copy that CreateHttp method into my ViewModel?

The WASM part is giving me an error:

The type or namespace name 'WasmHttpHandler' does not exist in the namespace 'unoDemo98.Wasm' (are you missing an assembly reference?)

I'm not getting any help with the quick actions . Any idea what I am missing?

        private static HttpClient CreateHttp()
        {
#if __WASM__
            var handler = new Wasm.WasmHttpHandler();
            var httpClient = new HttpClient(handler);
#else
            var httpClient = new HttpClient();

#endif
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
            return httpClient;
        }

@davidlsharp1 the WasmHttpHandler is part of the Uno.UI package. Is it included in your project ?

Hi @jeromelaban. Yes, I added the Uno.UI nuget.

@davidlsharp1 I missed the real reason. You have a namespace called Wasm and it's taking precedence over Uno.UI.Wasm. Try qualifying it completely Uno.UI.Wasm.WasmHttpHandler.

@jeromelaban Ah ok. I'll give it a try and report back. Thanks!

Support has significantly improved, and this issue has been superseded by https://github.com/nventive/Uno/issues/161

@jeromelaban , Hi. I'd like to see if I can get my rest client library working on Platform Uno:
https://github.com/MelbourneDeveloper/RestClient.Net

So, is this still to only way to construct a HttpClient?

var httpMessageHandler = Type
  .GetType("System.Net.Http.HttpClient, System.Net.Http")
  .GetField(
    "GetHttpMessageHandler",
    BindingFlags.Static | BindingFlags.NonPublic
  );

httpMessageHandler.SetValue(
  null,
  (Func<HttpMessageHandler>)(() => new Uno.UI.Wasm.WasmHttpHandler())
);

I'd be happy to write a version of RestClient.Net that wraps this code so that it's easy to hit a REST service. Is this the recommended approach?

I don't think it needs to be that complicated I have

#if __WASM__
            HttpClient = new HttpClient(new WasmHttpHandler());
#else
            HttpClient = new HttpClient(new HttpClientHandler());
#endif

Which is enough for UWP and WASM. Does that not work for ios/android?

@yowl that looks better.

@jeromelaban , how does building libraries from wasm via Uno work? Can Uno directly reference a DLL? A NuGet? Is there documentation on to do this?

I can't seem to get this to work at all.

Using the latest uno template I edit the simple mainpage with a button and label like so:

    public MainPage()
    {
        this.InitializeComponent();
        btn.Click += OnClicked;
    }

    private async void OnClicked(object sender, RoutedEventArgs e)
    {
        lbl.Text = "clicked";
        try
        {
            var wc = new System.Net.Http.HttpClient(new Uno.UI.Wasm.WasmHttpHandler());
            //var client = new System.Net.WebClient();
            var response = await wc.GetStringAsync(new Uri("http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html"));
            lbl.Text = response; 
        }
        catch(Exception ex)
        {
            lbl.Text = ex.Message;
        }
    }

I can't get any variation (including the above) of this to work at all for wasm. Am I missing something?

Never mind - I realize what happened. This works with Edge - not with the new chromium edge though (yet). Sorry.

Was this page helpful?
0 / 5 - 0 ratings