代码之家  ›  专栏  ›  技术社区  ›  Hunt

HttpClient在设置Timeout.Infinite后仍超时

  •  0
  • Hunt  · 技术社区  · 7 年前

    我正在使用 HttpClient 从中的服务器获取数据 Xamarin Forms 使用以下代码

    public  Task<HttpResponseMessage> sendData(String url,String jsonData)
        {
    
            using (var client = new HttpClient())
            {
    
                var jsonContent = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json");
    
                client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
                client.BaseAddress = new Uri(baseAddress);
    
                return  client.PostAsync(new Uri(url, UriKind.Relative), jsonContent);
            }
        }
    

    这里我用过 Timeout.Infinite 超时,无限 Operation time out 例外我尝试过各种各样的暂停,就像 240000ms 但仍然得到操作超时。

    然而,当我尝试在postman中触发相同的请求时,它正在工作,但在4分钟后返回响应。因为它是第三方api,所以我们不能在那里更改任何内容。

    有谁能建议我怎样才能强迫他们 HttpClient

    我得到了一个详细的例外

    {System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: The operation has timed out.
      at System.Net.HttpWebRequest+<RunWithTimeoutWorker>d__241`1[T].MoveNext () [0x000c5] in <3e9b3e26c4694baab3f689687ad40612>:0 
    --- End of stack trace from previous location where exception was thrown ---
      at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00020] in <3e9b3e26c4694baab3f689687ad40612>:0 
      at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <d4a23bbd2f544c30a48c44dd622ce09f>:0 
    --- End of stack trace from previous location where exception was thrown ---
      at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () [0x0041d] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0 
       --- End of inner exception stack trace ---
      at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () [0x00478] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0 
    --- End of stack trace from previous location where exception was thrown ---
      at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0 
    --- End of stack trace from previous location where exception was thrown ---
      at SignodeMSA.WebService.HttpService+<sendData>d__13.MoveNext () [0x0009c] in D:\VsualStudioWorkspace\SignodeMSA\SignodeMSA\SignodeMSA\WebService\HttpService.cs:141 
    --- End of stack trace from previous location where exception was thrown ---
      at SignodeMSA.WebService.RestApi+<sendDataSimple>d__96.MoveNext () [0x000b3] in D:\VsualStudioWorkspace\SignodeMSA\SignodeMSA\SignodeMSA\WebService\RestApi.cs:199 }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Hunt    7 年前

    我已经尝试过Android和iOS的以下代码,它似乎可以工作,并且在使用webservice获取数据时,可以在socket上等待更长的时间

    在里面 Xamarin.Android 使用以下代码,并使用 DependencyService

     public HttpClientHandler GetHttpClientHandler()
            {
    
                Xamarin.Android.Net.AndroidClientHandler http = new Xamarin.Android.Net.AndroidClientHandler();
                http.ReadTimeout = TimeSpan.FromMinutes(5.0);   
    
    
                return http;
            }
    

    Xamarin.iOS 使用以下代码,并使用 依赖服务

    HttpMessageHandler IHttpHandler.GetHttpHandler()
    {
    
    
        NSUrlSessionConfiguration sessionConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
        sessionConfig.TimeoutIntervalForRequest = 300;
        sessionConfig.TimeoutIntervalForResource = 300;
        sessionConfig.WaitsForConnectivity = true;
    
    
        NSUrlSessionHandler sessionHandler = new NSUrlSessionHandler(sessionConfig);
    
        return (sessionHandler);
    }
    

    HttpMessageHandler

    用于 Xamarin.Forms

       HttpClient client = new HttpClient(DependencyService.Get<IHttpClientHandler>().GetHttpClient());
    

    然而,这里有一个陷阱,在iOS的情况下,如果应用程序使用HttpClient获取数据,并且如果用户通过按iPhone中的锁定按钮锁定屏幕,那么服务会立即中断并抛出 Connection Interrupted

        2
  •  0
  •   Thomas De Wilde    7 年前

    尝试使用System.Threading.Timeout命名空间中的InfiniteTimeSpan。

    client.Timeout = System.Threading.Timeout.InfiniteTimeSpan;
    

    我正在我的Xamarin项目中使用它,它似乎工作得很好。