HttpClient的PostAsync方法在2-3次超时请求后抛出Aggregate异常

时间:2013-01-04 06:00:51

标签: c# windows c#-4.0 windows-8

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

2 个答案:

答案 0 :(得分:0)

HttpClient在引擎盖下使用HttpWebRequest。您可能正在达到HttpWebRequest的ServicePoint的两个并发连接限制。

答案 1 :(得分:0)

enter image description here

我在package.appxmanifest>中做了一些更改后遇到了同样的问题。功能

- >启用Internet(客户端和服务器)选项

此申请工作正常后。