我正在使用HttpClient与具有很多API的Web服务(由我的公司编写)进行交互。 所有api都运作良好,除非其中一个(较大和较慢)需要100秒钟以上的时间来给出答案,然后经过这段时间,我收到以下错误消息(注意:如果api所用时间少于100s,则一切正常): / p>
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
所有IIS和Web服务超时都设置为5分钟,所以我认为我可以排除这是Web服务超时问题,并且在应用程序上,我将HttpClient超时设置为10分钟(但是即使我设置了,它也无法正常工作) 5分钟),就像您在下面的代码中看到的一样。我在做什么错了?
代码:
var handler = new HttpClientHandler() { CookieContainer = cookie,
MaxRequestContentBufferSize = 256000000,
UseCookies = true,
};
HttpClient newclient = new HttpClient(handler);
newclient.Timeout = new TimeSpan(0, 10, 0); //That DIDN'T WORK :(
// ALL THE CODE I NEED TO BUILD MY JSON INSIDE jSoNToPost
//....
//....
//convert it to JSON acceptible content
HttpContent formContent = new StringContent(jSoNToPost, Encoding.UTF8, "application/json");
var uri = new Uri(Path.Combine(BaseUrl, "BIG_API_NAME"));
var response1 = await newclient.PostAsync(uri, formContent); // <-- THE EXCEPTION IS THROWN HERE!!!!!
if (response1.IsSuccessStatusCode)
{
//Other Stuff
//....
//....
}
else
{
//Other Stuff
//....
//....
}
环境:
Window 10,Visual Studio 2017,Xamarin IOS和Android。
EDIT 2019-01-23 14:21:
100s超时是默认的HttpClient超时,因此
newclient.Timeout = new TimeSpan(0, 10, 0);
不覆盖默认值。
我无法理解什么是InfiniteTimeSpan,因此我尝试遵循that post中的两个答案。没有成功:(它仍然会在100秒后显示超时错误。
答案 0 :(得分:0)
如果您在环境中使用负载平衡器,请务必检查是否在该级别设置了时间限制。作为安全措施,有些人会在某个时间点后切断会议。没有代码解决方案:(