Using HttpClient in the main Xamarin project

I am doing a project for several platforms, where I followed the best practices and created a main project containing all the code, which is the same on all platforms. To do network communication, I would like to use HttpClient, which seems to be the recommended method for exchanging information with web services in the future.

I DO NOT make a PCL, but just a standard C # library to contain the main project. However, it seems that there is no general implementation of HttpClient, but only for specific platforms. I cannot reference and use it in my kernel, but only in my Android and iOS projects.

I would really like to have all the network code in my main project, and as I see it, there are two options - none of which I like.

  • Create a common interface that I can use in the main project, which is implemented as a shell on both Android and iOS.
  • Use another technology that is supported in the main project - I think it is a web client or the like.

What is the recommended approach and why didn’t Hamarin make HttpClient available as a shared library?

Hope someone can help, Thanks

+7
source share
3 answers

I recommend checking out ModernHttpClient https://github.com/paulcbetts/modernhttpclient

This is a cross-platform network library for iOS and Android. From the project:

This library features the latest networking libraries specific to the Xamarin platform platform through the HttpClient user-defined handler. Write your own application using System.Net.Http, but drop this library and it will go much faster.

+7
source

You should be able to use System.Net.Http in your main library just fine, I did it recently.

Make sure your main library is a .Net 4.5 project. There is a parameter in the project parameters for selecting the .Net version. After checking this, all you have to do is reference System.Net.Http.dll .

If this does not solve your problem, are you working on Windows or Mac (Xamarin Studio)?

+2
source

I am afraid that you cannot use HttpClient on Mono at the moment due to licensing restrictions. There is a UserVoice offer in which you can vote if you want.

+1
source

All Articles