我正在使用 HttpClient 进行网络请求,它涉及“超时”功能,除了超时功能外,一切正常
当请求被击中 2-3 次时会引发“聚合异常”(例如单击登录按钮并且请求超时)。
我试过“捕捉”异常,但它不起作用。
使用的代码:
try
{
HttpClient httpClient = new HttpClient();
//explicit timeout for testing
TimeSpan requestTimeout = new TimeSpan(1000);
httpClient.Timeout = requestTimeout;
HttpContent httpContent = new StringContent(postJSON);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = null;
response = await httpClient.PostAsync(url, httpContent);
if (response != null)
{
response.EnsureSuccessStatusCode();
netResults = await response.Content.ReadAsStringAsync();
Logger.Log("NetworkRequest:ResponseStream:Json result:" + netResults);
}
if (this.convertedType != null)
{
MemoryStream assetReader = GetMemoryStreamFromString(netResults);
assetReader.Position = 0;
object value = fromJSON(assetReader, this.convertedType);
networkReqSuccessWithObjectCallback(this, value);
}
else
{
//Return netResult as string.
networkReqSuccessWithStringCallback(this, netResults);
}
}
catch (TaskCanceledException)
{
ErrorException ee = null;
ee = new ErrorException("RequestTimeOut");
NotifyNetworkDelegates(ee);
}
catch (WebException we)
{
// failure
ErrorException ee = null;
ee = ErrorException.fromJSON(we.Message);
NotifyNetworkDelegates(ee);
}
catch (Exception e)
{
Do something.
}
关于这里出了什么问题的任何想法。(使用 C#+XAML+Win8)