简体   繁体   English

Xamarin.Forms - HttpClient SendAsync中未处理的异常

[英]Xamarin.Forms - unhandled exception at HttpClient SendAsync

I'm facing an exception when trying to send a HTTP request on Android. 尝试在Android上发送HTTP请求时,我遇到异常。 On WinPhone, it works. 在WinPhone上,它的工作原理。 This is my code: 这是我的代码:

        string resp = null;

        using (var client = new HttpClient())
        {
            var httpRequest = new HttpRequestMessage(new HttpMethod("POST"), _uri);
            //client.BaseAddress = new Uri(_uri);
            try
            {
                client.Timeout = TimeSpan.FromSeconds(30);
                var _cancelTokenSource = new CancellationTokenSource();
                var _cancelToken = _cancelTokenSource.Token;
                var response = await client.SendAsync(httpRequest, _cancelToken);
                resp = await response.Content.ReadAsStringAsync();
                //var result = await client.PostAsync("", new StringContent(json.ToString(), Encoding.UTF8, "application/json"));
                //resp = await result.Content.ReadAsStringAsync();
            }
            catch (Exception e)
            {
                Debug.WriteLine("HTTP ERROR: " + e.Message);
                throw e;
            }
        }

On Properties > Android Manifest I've checked INTERNET for permission. 在属性> Android清单上我已经检查了INTERNET的许可。 Additionally, it goes through an unhandled exception and I cannot see what happed. 此外,它经历了一个未处理的异常,我无法看到发生了什么。 It didn't show the exactly error, neither arriver on my method for unhandled exception that I've set like this: 它没有显示确切的错误,也没有出现在我设置的未处理异常的方法上,如下所示:

AndroidEnvironment.UnhandledExceptionRaiser += AppDroid_UnhandledExceptionHandler;

Any idea about what is wrong or any idea about how to see the error message? 是否有任何关于如何查看错误消息的错误或想法? On WinPhone it arrive on my method when some unexpected error occurs, then it's easy to fix. 在WinPhone上,当出现一些意外错误时,它会到达我的方法,然后很容易修复。

Thanks! 谢谢!

There's a known bug on Android HttpClient. Android HttpClient上存在一个已知错误。 See this: http://forums.xamarin.com/discussion/36470/httpclient-postasync-deadlock-system-net-sockets-connect-internal 见: http//forums.xamarin.com/discussion/36470/httpclient-postasync-deadlock-system-net-sockets-connect-internal

Change it to: 将其更改为:

using (var client = new HttpClient())
        {
            var httpRequest = new HttpRequestMessage(new HttpMethod("POST"), _uri);
            //client.BaseAddress = new Uri(_uri);
            try
            {
                client.Timeout = TimeSpan.FromSeconds(30);
                var _cancelTokenSource = new CancellationTokenSource();
                var _cancelToken = _cancelTokenSource.Token;
                var response = await client.SendAsync(httpRequest, _cancelToken).ConfigureAwait(false);
                resp = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("HTTP ERROR: " + e.Message);
                throw e;
            }
        }

You could also try ModernHttpClient. 您也可以尝试ModernHttpClient。 It's a direct replacement. 这是一个直接的替代品。

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

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