即使设置了Timeout.Infinite后,HttpClient也变得超时

时间:2019-01-23 07:15:52

标签: .net xamarin xamarin.forms dotnet-httpclient

我正在使用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是因为Web服务花费了大约4分钟来返回响应,但是尽管我已将Timeout.Infinite应用设置为抛出Operation time out异常。我尝试了240000ms之类的各种超时方法,但是仍然遇到了操作超时问题。

但是,当我尝试在邮递员中触发相同的请求时,它正在工作,但是可以,在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 个答案:

答案 0 :(得分:0)

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

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

我在我的Xamarin项目中使用了它,并且看起来工作正常。

答案 1 :(得分:0)

我尝试了以下针对Android和iOS的代码,它似乎可以正常工作,并且在使用Web服务获取数据时可以在套接字上等待更长的时间

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中使用

以下代码,并使用DependencyService

公开它
HttpMessageHandler IHttpHandler.GetHttpHandler()
{


    NSUrlSessionConfiguration sessionConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
    sessionConfig.TimeoutIntervalForRequest = 300;
    sessionConfig.TimeoutIntervalForResource = 300;
    sessionConfig.WaitsForConnectivity = true;


    NSUrlSessionHandler sessionHandler = new NSUrlSessionHandler(sessionConfig);

    return (sessionHandler);
}

注意:如果使用iOS,它将返回HttpMessageHandler

Xamarin.Forms中使用

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

但是,这里有一个问题,在iOS上,如果应用程序正在使用HttpClient来获取数据,并且如果用户通过按iPhone中的“锁定”按钮锁定了屏幕,则该服务将立即中断并抛出Connection Interrupted和套接字被关闭。如果有人可以找到解决方案,请随时发布