我正在使用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)