System.MissingMethodException 方法 'System.Net.Http.HttpClientHandler.set_Proxy' 未找到
System.MissingMethodException Method 'System.Net.Http.HttpClientHandler.set_Proxy' not found
这是一个 Xamarin 解决方案,我收到了此消息标题中的错误。当然,我可以很容易地确认PCL项目中的HttpClientHandler上有一个Proxy属性。解决方案构建无误。只有当我 运行 时它才会产生这个错误(在 Droid 或 iOS 上)并且在它调用实例化 HttpClient 的 PCL 中的方法时发生。请注意,它甚至没有到达该方法。错误出现在application start-up 方法上;例如,UIApplication.Main()
如果我注释掉处理程序并在没有处理程序的情况下实例化 HttpClient,只要我在开放的互联网上,它就可以正常工作。但我正试图让它在代理后面工作。
进一步调查表明设备项目没有引用 System.Net.Http。所以我添加了这些 - 它指示 Xamarin.iOS 和 Xamarin.Android 作为包 - 但它仍然产生错误。
我不清楚错误告诉我什么,但我相信这意味着设备项目看不到 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 package to your platform project too. If you run into an issue adding this, try installing the latest Microsoft.Bcl.Build
package。然后,安装之后,添加HTTP包。
这是一个 Xamarin 解决方案,我收到了此消息标题中的错误。当然,我可以很容易地确认PCL项目中的HttpClientHandler上有一个Proxy属性。解决方案构建无误。只有当我 运行 时它才会产生这个错误(在 Droid 或 iOS 上)并且在它调用实例化 HttpClient 的 PCL 中的方法时发生。请注意,它甚至没有到达该方法。错误出现在application start-up 方法上;例如,UIApplication.Main()
如果我注释掉处理程序并在没有处理程序的情况下实例化 HttpClient,只要我在开放的互联网上,它就可以正常工作。但我正试图让它在代理后面工作。
进一步调查表明设备项目没有引用 System.Net.Http。所以我添加了这些 - 它指示 Xamarin.iOS 和 Xamarin.Android 作为包 - 但它仍然产生错误。
我不清楚错误告诉我什么,但我相信这意味着设备项目看不到 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 package to your platform project too. If you run into an issue adding this, try installing the latest Microsoft.Bcl.Build
package。然后,安装之后,添加HTTP包。