1

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

4

2 回答 2

0

HttpClient 在后台使用 HttpWebRequest。您很可能达到了 HttpWebRequest 的 ServicePoint 的两个并发连接限制。

于 2013-01-04T06:07:17.617 回答
0

在此处输入图像描述

对 package.appxmanifest > Capabilities 进行一些更改后,我遇到了同样的问题

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

此应用程序工作正常后。

于 2015-12-29T14:57:03.863 回答