即使在设置 Timeout.Infinite 后 HttpClient 也会超时
HttpClient getting timed out even after setting Timeout.Infinite
我正在使用 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
,因为网络服务需要大约 4 分钟才能 return 响应,但尽管我已经设置 Timeout.Infinite
应用程序抛出 Operation time out
异常。我尝试了各种超时以及 240000ms
但仍然出现操作超时。
但是,当我尝试在邮递员中发出相同的请求时,它正在工作,但是 return 在 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 }
尝试使用 System.Threading.Timeout 命名空间中的 InfiniteTimeSpan。
client.Timeout = System.Threading.Timeout.InfiniteTimeSpan;
我在我的 Xamarin 项目中使用它,它似乎工作正常。
我已经为 Android 和 iOS 尝试了以下代码,它似乎可以工作,并且在使用 webservice
获取数据时可以在套接字上等待更长时间
在 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 的情况下它将 return HttpMessageHandler
在Xamarin.Forms
中的用法
HttpClient client = new HttpClient(DependencyService.Get<IHttpClientHandler>().GetHttpClient());
但是,这里有一个问题,如果 iOS 如果应用正在使用 HttpClient 获取数据,并且如果用户通过按 iPhone 中的锁定按钮锁定屏幕,则服务立即被打断并抛出 Connection Interrupted
并关闭套接字。如果有人能找到任何解决方案,请随时 post
我正在使用 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
,因为网络服务需要大约 4 分钟才能 return 响应,但尽管我已经设置 Timeout.Infinite
应用程序抛出 Operation time out
异常。我尝试了各种超时以及 240000ms
但仍然出现操作超时。
但是,当我尝试在邮递员中发出相同的请求时,它正在工作,但是 return 在 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 }
尝试使用 System.Threading.Timeout 命名空间中的 InfiniteTimeSpan。
client.Timeout = System.Threading.Timeout.InfiniteTimeSpan;
我在我的 Xamarin 项目中使用它,它似乎工作正常。
我已经为 Android 和 iOS 尝试了以下代码,它似乎可以工作,并且在使用 webservice
获取数据时可以在套接字上等待更长时间在 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 的情况下它将 return HttpMessageHandler
在Xamarin.Forms
HttpClient client = new HttpClient(DependencyService.Get<IHttpClientHandler>().GetHttpClient());
但是,这里有一个问题,如果 iOS 如果应用正在使用 HttpClient 获取数据,并且如果用户通过按 iPhone 中的锁定按钮锁定屏幕,则服务立即被打断并抛出 Connection Interrupted
并关闭套接字。如果有人能找到任何解决方案,请随时 post