简体   繁体   English

HttpClient 的 PostAsync 方法在 2-3 个超时请求后抛出聚合异常

[英]PostAsync method of HttpClient throws an Aggregate exception after 2-3 timeout requests

I am making a webrequest using HttpClient and it involves "Timeout" feature, everything works fine apart from the timeout feature我正在使用 HttpClient 进行网络请求,它涉及“超时”功能,除了超时功能外,一切正常

which throws an "Aggregate Exception" when a request is hit 2-3 times(like click on Sign In button and request gets timed out).当请求被击中 2-3 次时会引发“聚合异常”(例如单击“登录”按钮并且请求超时)。

i ve tried "Catching" the exception but it doesnt work.我试过“捕捉”异常,但它不起作用。

Code used :使用的代码:

        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.
        }

Any idea on what is going wrong here.(using C#+XAML+Win8)关于这里出了什么问题的任何想法。(使用 C#+XAML+Win8)

HttpClient uses HttpWebRequest under the hood. HttpClient 在后台使用 HttpWebRequest。 It's likely that you're hitting the two concurrent connection limit of the HttpWebRequest's ServicePoint.您很可能达到了 HttpWebRequest 的 ServicePoint 的两个并发连接限制。

在此处输入图片说明

I got same issues once I do some change in package.appxmanifest > Capabilities我在 package.appxmanifest > Capabilities 中做了一些更改后遇到了同样的问题

-> Enable Internet (Client & Server) option -> 启用 Internet(客户端和服务器)选项

After this application working fine.在此应用程序工作正常后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM