HttpClient在Android上不起作用 [英] HttpClient not working on android

查看:155
本文介绍了HttpClient在Android上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作Xamarin.Forms应用程序,该应用程序应从api获取JSON,然后允许显示它.到目前为止,我的代码:

I'm making Xamarin.Forms app which should get JSON from api and then allow to display it. My code so far:

    public async void jsonDownload()
    {
        connect();
        await downloadData();


    }
    public void connect()
    {
        client = new HttpClient();
        client.MaxResponseContentBufferSize = 256000;
    }
    public async Task<List<Jsonclass>> downloadData()
    {

        String url = "https://my-json-server.typicode.com/kgbzoma/TestJsonFile/all";
        var uri = new Uri(string.Format(url, string.Empty));
        try
        {

            var response = await client.GetAsync(uri).ConfigureAwait(false);
            response.EnsureSuccessStatusCode(); //NEVER GET HERE

            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); 
            List = JsonConvert.DeserializeObject<List<Jsonclass>>(content);

        }catch (Exception ex)
        {
            Debug.WriteLine(@"     Error {0}", ex.Message);
        }
        return List;
    }

问题是代码甚至没有响应.EnsureSuccessStatusCode();所以我的对象列表是空的.在UWP版本上,它可以正常工作. 这是我的异常:带有消息的System.Net.Http.HttpRequestException发送请求时发生错误.

Problem is that code don't even go to response.EnsureSuccessStatusCode(); so my list of objects is empty. On UWP version it's working without any problems. Here i'm gettin exception: System.Net.Http.HttpRequestException with message An error occurred while sending the request.

推荐答案

SecureChannelFailure(身份验证或解密失败.)

SecureChannelFailure (The authentication or decryption has failed.)

System.Net.WebException:错误:TrustFailure

System.Net.WebException: Error: TrustFailure

Mono.Security.Protocol.Tls.TlsException:从服务器接收到无效的证书.

Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server.

默认情况下,Xamarin.Android使用不支持TLS 1.2的较旧的Mono Managed HttpClient处理程序.

By default, Xamarin.Android uses the older Mono Managed HttpClient handler that does not support TLS 1.2.

打开您的Xamarin.Android项目设置,转到Build/Android Build/General并使用AndroidClientHandler.

Open your Xamarin.Android project settings, goto Build / Android Build / General and use the AndroidClientHandler.

这会将以下MSBuild属性直接添加到您的.csproj

This will add the following MSBuild properties directly to your .csproj

<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
<AndroidTlsProvider>btls</AndroidTlsProvider>

注意:如果在.csproj中手动执行此操作,则需要将它们添加到调试并释放PropertyGroup.

Note: If doing this manually in the .csproj, you need to add them to the debug AND release PropertyGroup.

或以编程方式将HttpClient设置为使用它:

Or programmatically set the HttpClient to use it:

client = new HttpClient(new AndroidClientHandler());

注意:您应该查看InnerException这些类型的错误

Note: You should be looking at the InnerException for these types of errors

catch (Exception ex)
{
    Console.WriteLine(@"     Error {0}", ex.Message);
    Console.WriteLine(@"     Error {0}", ex.InnerException?.Message);
}

这篇关于HttpClient在Android上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆