找不到 System.MissingMethodException 方法“System.Net.Http.HttpClientHandler.set_Proxy" [英] System.MissingMethodException Method 'System.Net.Http.HttpClientHandler.set_Proxy' not found

查看:49
本文介绍了找不到 System.MissingMethodException 方法“System.Net.Http.HttpClientHandler.set_Proxy"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 Xamarin 解决方案,我在此消息的标题中发现了错误.当然,我可以轻松确认PCL项目中HttpClientHandler上有Proxy属性.并且解决方案构建没有错误.只有当我运行时,它才会产生这个错误(在 Droid 或 iOS 上),并且在它调用 PCL 中实例化 HttpClient 的方法时这样做.请注意,它甚至没有达到该方法.错误出现在应用程序启动方法上;例如,UIApplication.Main()

This is a Xamarin solution and I am getting the error found in this message's title. Of course, I can easily confirm that there is a Proxy property on HttpClientHandler in the PCL project. And the solution builds without error. Only when I run does it produce this error (on either Droid or iOS) and does so at the point where it invokes the method in the PCL which instantiates the HttpClient. Note that it doesn't even get to that method. The error appears on the application start-up method; e.g., UIApplication.Main()

如果我注释掉处理程序并在没有处理程序的情况下实例化 HttpClient,只要我在开放的互联网上,它就可以正常工作.但我正试图让它从代理后面工作.

If I comment out the handler and instantiate HttpClient without a handler, it works fine as long as I'm on the open internet. But I'm trying to get this to work from behind a proxy.

进一步调查显示设备项目没有对 System.Net.Http 的引用.所以我添加了这些——它表明 Xamarin.iOS 和 Xamarin.Android 作为包——但它仍然产生错误.

Further investigation showed that the device projects had no references to System.Net.Http. So I added these -- and it indicates Xamarin.iOS and Xamarin.Android as the packages -- but it still produces the error.

我不清楚错误告诉我什么,但我相信这意味着设备项目看不到 System.Net.Http.HttpClientHandler?

I'm not clear what the error is telling me but I believe it means that the device project can't see System.Net.Http.HttpClientHandler?

    private HttpClient GetHttpClient()
    {
        WebProxy proxy = new WebProxy(ProxyConfig.Url)
        {
            Credentials = new NetworkCredential(ProxyConfig.Username, ProxyConfig.Password)
        };

        // At runtime, when GetHttpClient is invoked, it says it cannot find the Proxy setter
        HttpClientHandler handler = new HttpClientHandler
        {
            Proxy = proxy,
            UseProxy = true,
            PreAuthenticate = true,
            UseDefaultCredentials = false,
        };
        HttpClient client = new HttpClient(handler);

        // This works when not behind a proxy
        //HttpClient client = new HttpClient();

        return client;
    }

    public async Task GetWeatherAsync(double longitude, double latitude, string username)
    {

        // MissingMethodException is thrown at this point
        var client = GetHttpClient();
        client.BaseAddress = new Uri(string.Format("http://api.geonames.org/findNearByWeatherJSON?lat={0}&lng={1}&username={2}", latitude, longitude, username));

        try
        {
            var response = await client.GetAsync(client.BaseAddress);
            if (response.IsSuccessStatusCode)
            {
                var JsonResult = response.Content.ReadAsStringAsync().Result;
                var weather = JsonConvert.DeserializeObject<WeatherResult>(JsonResult);

                SetValues(weather);
            }
            else
            {
                Debug.WriteLine(response.RequestMessage);
            }
        }
        catch (HttpRequestException ex)
        {
            Debug.WriteLine(ex.Message);
        }
        catch (System.Net.WebException ex)
        {
            Debug.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }

推荐答案

添加 Microsoft.Net.Http NuGet 包 到您的平台项目.如果您在添加此内容时遇到问题,请尝试安装最新的 Microsoft.Bcl.Build 首先打包.然后,安装完成后,添加HTTP包.

Add the Microsoft.Net.Http NuGet package to your platform project too. If you run into an issue adding this, try installing the latest Microsoft.Bcl.Build package first. Then, after that is installed, add the HTTP package.

这篇关于找不到 System.MissingMethodException 方法“System.Net.Http.HttpClientHandler.set_Proxy"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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