System.Net.Http.HttpClient not Respect timeouts and use everytime the default Value

空扰寡人 提交于 2019-12-23 19:21:23

问题


I'm using HttpClient to interact with a webservice (written by my Company) with a lot of apis. All the apis work great except when one of that (the bigger and slower) take more than 100 seconds to give an answer, passed that time i receive the following error(N.B: If the api take less than 100s all works well):

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 

All the IIS and webservice Timeouts are setted to 5 minutes, so i think i can exclude that is a Webservice Timeout Problem and on the app i set the HttpClient timeout to 10 minutes (but it doesn't work even if i set it at 5 mins), like you can see in my code below. What i'm doing wrong?

CODE:

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
    //....
    //....
}

ENVIRONMENT:

Window 10, Visual Studio 2017, Xamarin IOS and Android.

EDIT 2019-01-23 14:21:

The 100s timeout is the default HttpClient timeout, so the

newclient.Timeout = new TimeSpan(0, 10, 0);

not overwrite the default value.


I undestand what is InfiniteTimeSpan and i try to follow both the answers in that post. No success :( it Still show the timeout error after 100s.

来源:https://stackoverflow.com/questions/54326681/system-net-http-httpclient-not-respect-timeouts-and-use-everytime-the-default-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!