Droid项目中引用的.NET Standard 2库中的HttpClient实现

时间:2018-10-04 13:19:46

标签: c# android xamarin.forms .net-standard-2.0

我在.NET Standard 2库中有一个HttpClient实例,Droid项目引用了该库。在Droid项目中,将HttpClient实现设置为Android。 HttpClient实现将使用java.net.URLConnection还是HttpClientHandler?

Android Options

我希望它使用本机HttpClient。如果它使用托管的HttpClientHandler,我是否需要在我的共享代码库中为Android添加一个特殊情况,如下所示:

// if we're on any other platform
HttpClient client = new HttpClient()
// if we're on Android    
HttpClient client = new HttpClient(new Xamarin.Android.Net.AndroidClientHandler ())

https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows

1 个答案:

答案 0 :(得分:0)

如果您在项目选项中指定客户端的类型,则只需初始化HttpClient(),而无需指定其类型。

从文档中

// This will use the default message handler for the application; as
// set in the Project Options for the project.
HttpClient client = new HttpClient();

// This will create an HttpClient that explicitly uses the CFNetworkHandler
HttpClient client = new HttpClient(new CFNetworkHandler());

// This will create an HttpClient that explicitly uses NSUrlSessionHandler
HttpClient client = new HttpClient(new NSUrlSessionHandler());