简体   繁体   English

在 iOS 中的 Xamarin Forms 中创建一个 HttpClient

[英]Create a HttpClient in Xamarin Forms in iOS

I have an ASP.Net Core API that is running.我有一个正在运行的 ASP.Net Core API。

I have a Xamarin Forms solution and in the main project and I have this class:我有一个 Xamarin Forms 解决方案,在主项目中,我有这个类:

public class GetSettings
{

    public static async Task<SettingResponse> GetSetting()
    {
        SettingResponse setting = new SettingResponse();

        var url = new Uri(string.Format(GetUrl.GenerateURL("GetSetting"), string.Empty));

        var client = new HttpClient();

        var response = await client.GetAsync(url);

        if (response.IsSuccessStatusCode)
        {
            var result = response.Content.ReadAsStringAsync().Result;

            setting = JsonConvert.DeserializeObject<SettingResponse>(result);
        }
        else
        {
            setting = null;
        }

        return setting;
    }
}

In the Android and iOS project I have this two lines:在 Android 和 iOS 项目中,我有这两行:

SettingResponse setting = new SettingResponse();

setting = await GetSettings.GetSetting();

In the Android project there is no problem and it works fine, but in the iOS project when getting to new HttpClient();在Android项目中没有问题并且工作正常,但在iOS项目中使用new HttpClient(); nothing happens and app does nothing.什么都没有发生,应用程序什么也不做。

Check if you have the NSAllowsArbitraryLoads key to YES in NSAppTransportSecurity dictionary in your info.plist file.检查您的 info.plist 文件中的 NSAppTransportSecurity 字典中的 NSAllowsArbitraryLoads 键是否为 YES。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM