Using HttpClient PCL (Microsoft.Net.Http) in Xamarin Android

I used to use HttpWebRequest in my library (PCL), but since HttpClient is now fully supported across platforms, I decided to revise my code to use HttpClient. On Windows platorms everything works fine, but when I build a test app for Android, it hangs in the simulator. Perhaps I am doing something wrong, so I'd like to clarify a few things.

  1. The Windows apps reference the following NuGet packages:
  • Microsoft.Net.Http
  • Microsoft.Bcl.Build
  • Microsoft.Bcl
  • Microsoft.Bcl.Async (on some platforms)

Do you I need to reference any of those NuGet packages on Mono? I checked portable HttpClient Xamarin sample, and the test app doesn't reference any of those. Are there any circumstances when some of these packages need to be referenced by the host app? My PCL references them, will it be sufficient?

  1. When I build the Android app, I get warnings that the project must install package Microsoft.Net.Http and Microsoft.Bcl.Async. And if I do, it doesn't compile complaining about duplicate references to some assemblies (e.g. System.Runtime)

  2. Are there any known obstacles/problems with using HttpClient from a PCL on Mono?

It would be great to find an example of an application that uses a PCL referencing HttpClient via NuGet. A Xamarin HttpClient references HttpClient directly.

Thanks in advance

Posts

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    Depending on the profile you are using, you may/may not have to include the BCL library. AFAIK there's only a few problems when using HttpClient and some of those issues can be solved by ModernHttpClient by Paul Betts. You can also find this in the component store with a few samples.

    https://github.com/paulcbetts/ModernHttpClient/tree/master/samples

    I would recommend trying to make a PCL with Profile 78, and doing the same thing you're after. You can check the profile number by opening up your Core.csproj into Notepad and reading the XML. You can then use regular HttpClient, or implement ModernHttpClient into your application.

    Sounds like you may just be running into a reference issue, but after installing the BCL you should be set to go with your profile.

  • Thanks for a quick response. I've heard about ModernHttpClient, but I don't understand how I can replace default HttpClient with that one. In my PCL I simply references Microsoft.Net.Http. Is there something I should change in my library nuspec file in order to resolve HttpClient to ModernHttlClient on the specific platform?

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    ModernHttpClient will override the default httpclient with a more suited one on each platform. For example Okhttp and AFnetworking. However httpclient itself should work fine on each platform, it just may be slower on some.

  • Thanks, I've found some tips regarding how to use ModernHttpClient from a PCL. I alo modified the original Xamarin HttpClient sample to use different PCL profiles. It all worked, so apparently the problem is in my code.

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    Hooray!

  • Vagif Abilov

    I am also struggling with System.Net.HTTP, could you share the code, how you resolved it using ModernHttpClient.

    Thanks
    Email : [email protected]

Sign In or Register to comment.