使用httpclient时出现系统异常
System exception when using httpclient
我的通用应用程序项目 (Universal Windows 8.1) 有问题。我想下载一个字符串,然后将其反序列化为 类。但我鼓励在 windows phone 上下载此字符串时遇到问题。我有一个单独的库。在我只有 httpclient 的地方,它下载字符串,然后将其反序列化为 类。一切正常(测试为绿色),但是当我将它插入 windows phone 应用程序项目时。看起来,它进入库代码,但不下载任何东西..
我移动代码以将字符串下载到 windows phone 解决方案以检查它为什么不起作用。但是现在我经常遇到这样的异常:
System.Exception: Exception from HRESULT: 0x80072F30
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ProjectName.Mobile.MainPage.<InitComboBoxes>d__10.MoveNext()
这是我的代码:
private HttpClient client;
private CancellationTokenSource cts;
private HttpBaseProtocolFilter filter;
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
filter = new HttpBaseProtocolFilter();
client = new HttpClient(filter);
cts = new CancellationTokenSource();
await InitComboBoxes();
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
filter?.Dispose();
client?.Dispose();
cts?.Dispose();
}
private async Task InitComboBoxes()
{
try
{
var response = await client.GetAsync(new Uri(@"http://planer.info.pl/api/rest/places.json")).AsTask(cts.Token);
var t = await response.Content.ReadAsStringAsync().AsTask(cts.Token);
}
catch (Exception e)
{
var msg = new MessageDialog("Please check Your internet connection!" + e.Message);
await msg.ShowAsync();
}
}
现在我不知道如何解决这个问题..如果有人能帮助我,我将不胜感激!
如果您只想从 url 获取数据,请尝试使用此代码。除非有什么原因这不符合您的需求?
public async Task<string> GetData()
{
using (var client = new HttpClient())
{
try
{
HttpResponseMessage responseMessage = client.GetAsync("http://planer.info.pl/api/rest/places.json").Result;
if (responseMessage.IsSuccessStatusCode)
return await responseMessage.Content.ReadAsStringAsync();
}
catch (Exception e)
{
}
}
return null;
}
String response = await GetData();
看看你的代码,我没有发现它有什么特别的问题(而且我没有测试它);除了:
HttpResponseMessage
是 IDisposable
所以你也应该考虑处理它。
但是,这只是一些批评:P
查看异常,根据 MSDN 问题是您无法连接到服务器。
ERROR_WINHTTP_NO_CM_CONNECTION
0x80072F30
There was a problem connecting to the server.
出现此问题的方式有很多种:
- 证书
- 防火墙
- 服务器关闭
使用 Phone 时,请确保 phone 上的时间设置正确(我发现这会导致很多问题)
它进入库代码,但不下载任何东西..
我不确定你的意思,但如果你的意思是它进入但没有 return 那么我怀疑线程 deadlock。
希望对您有所帮助。
我的通用应用程序项目 (Universal Windows 8.1) 有问题。我想下载一个字符串,然后将其反序列化为 类。但我鼓励在 windows phone 上下载此字符串时遇到问题。我有一个单独的库。在我只有 httpclient 的地方,它下载字符串,然后将其反序列化为 类。一切正常(测试为绿色),但是当我将它插入 windows phone 应用程序项目时。看起来,它进入库代码,但不下载任何东西.. 我移动代码以将字符串下载到 windows phone 解决方案以检查它为什么不起作用。但是现在我经常遇到这样的异常:
System.Exception: Exception from HRESULT: 0x80072F30
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ProjectName.Mobile.MainPage.<InitComboBoxes>d__10.MoveNext()
这是我的代码:
private HttpClient client;
private CancellationTokenSource cts;
private HttpBaseProtocolFilter filter;
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
filter = new HttpBaseProtocolFilter();
client = new HttpClient(filter);
cts = new CancellationTokenSource();
await InitComboBoxes();
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
filter?.Dispose();
client?.Dispose();
cts?.Dispose();
}
private async Task InitComboBoxes()
{
try
{
var response = await client.GetAsync(new Uri(@"http://planer.info.pl/api/rest/places.json")).AsTask(cts.Token);
var t = await response.Content.ReadAsStringAsync().AsTask(cts.Token);
}
catch (Exception e)
{
var msg = new MessageDialog("Please check Your internet connection!" + e.Message);
await msg.ShowAsync();
}
}
现在我不知道如何解决这个问题..如果有人能帮助我,我将不胜感激!
如果您只想从 url 获取数据,请尝试使用此代码。除非有什么原因这不符合您的需求?
public async Task<string> GetData()
{
using (var client = new HttpClient())
{
try
{
HttpResponseMessage responseMessage = client.GetAsync("http://planer.info.pl/api/rest/places.json").Result;
if (responseMessage.IsSuccessStatusCode)
return await responseMessage.Content.ReadAsStringAsync();
}
catch (Exception e)
{
}
}
return null;
}
String response = await GetData();
看看你的代码,我没有发现它有什么特别的问题(而且我没有测试它);除了:
HttpResponseMessage
是IDisposable
所以你也应该考虑处理它。
但是,这只是一些批评:P
查看异常,根据 MSDN 问题是您无法连接到服务器。
ERROR_WINHTTP_NO_CM_CONNECTION
0x80072F30
There was a problem connecting to the server.
出现此问题的方式有很多种:
- 证书
- 防火墙
- 服务器关闭
使用 Phone 时,请确保 phone 上的时间设置正确(我发现这会导致很多问题)
它进入库代码,但不下载任何东西..
我不确定你的意思,但如果你的意思是它进入但没有 return 那么我怀疑线程 deadlock。
希望对您有所帮助。