Skip to content

Getting HttpClient to work with Wasm #42

Closed

Description

@davidlsharp1

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?

Activity

  1. zbigniew-gajewski commented on Jun 15, 2018

    @zbigniew-gajewski

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

  2. davidlsharp1 commented on Jun 16, 2018

    @davidlsharp1
    Author

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

  3. davidlsharp1 commented on Jun 16, 2018

    @davidlsharp1
    Author

    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;
            }
    
  4. jeromelaban commented on Jun 18, 2018

    @jeromelaban
    Member

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

  5. davidlsharp1 commented on Jun 18, 2018

    @davidlsharp1
    Author

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

  6. jeromelaban commented on Jun 18, 2018

    @jeromelaban
    Member

    @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.

  7. davidlsharp1 commented on Jun 18, 2018

    @davidlsharp1
    Author

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

  8. jeromelaban commented on Oct 2, 2018

    @jeromelaban
    Member

    Support has significantly improved, and this issue has been superseded by #161

  9. MelbourneDeveloper commented on Feb 17, 2019

    @MelbourneDeveloper

    @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?

  10. yowl commented on Feb 18, 2019

    @yowl
    Contributor

    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?

  11. MelbourneDeveloper commented on Feb 18, 2019

    @MelbourneDeveloper

    @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?

  12. garethjhewitt commented on Jun 27, 2019

    @garethjhewitt

    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.

  13. added a commit that references this issue on Nov 14, 2021
  14. added a commit that references this issue on Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions