簡體   English   中英

GetAsync導致超時問題

[英]GetAsync causing timeout issue

我有下面的代碼,在獲取數據時會導致問題

 var result = client.GetAsync(requestUri).Result;

如果我從瀏覽器中命中了requestUri ,那么它將正常工作。 但從上面的代碼,它給出了下面的錯誤。

錯誤:

發生一個或多個錯誤。發送請求時發生錯誤。基礎連接已關閉:發送時發生意外錯誤。由於遠程方已關閉傳輸流,因此身份驗證失敗。

堆棧跟蹤1:

在System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at foo(String resultUrl) at dooo(String param, String resultUrl) at foo.<>c__DisplayClass2.<Product>b__0(String x, String y) at foo[T](Func System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)的System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)的1.GetResultCore(Boolean waitCompletionNotification) at foo(String resultUrl) at dooo(String param, String resultUrl) at foo.<>c__DisplayClass2.<Product>b__0(String x, String y) at foo[T](Func 3 fn) .ConnectStream.WriteHeadersCallback(IAsyncResult ar)

堆棧跟蹤2

在System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at Foo1(String resultUrl) at Foo2(String param, String resultUrl) at Foo3.<>c__DisplayClass2.<Product>b__0(String x, String y) at Foo4(Func System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)位於System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)位於System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 1.GetResultCore(Boolean waitCompletionNotification) at Foo1(String resultUrl) at Foo2(String param, String resultUrl) at Foo3.<>c__DisplayClass2.<Product>b__0(String x, String y) at Foo4(Func 3 fn)。 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()上的WriteHeadersCallback(IAsyncResult ar)-從上次引發異常的位置開始的堆棧跟蹤-在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任務)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()---從上一個引發異常的位置開始的堆棧跟蹤結束--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)位於System.Runtime System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()位於System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()處的.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

我也嘗試了等待的電話,但沒有運氣。 在這種情況下,我們可以增加獲取請求的請求超時時間嗎?

我嘗試的等待代碼

var result = await client.GetAsync(requestUri);
                if (!result.IsSuccessStatusCode) throw new Exception(string.Format("Unable to receive data from Uri: {0}", uri.ToString()));

                // write the results
                using (var write = File.Create(filePath))
                using (var read = await result.Content.ReadAsStreamAsync())
                {
                    read.Seek(0, SeekOrigin.Begin);
                    read.CopyTo(write);
                }

如果響應時間很長,並且您想增加超時時間,則只需在客戶端上進行設置即可: https : //msdn.microsoft.com/zh-cn/library/system.net.http.httpclient.timeout(v=vs .118).aspx

同樣從查看類本身來看:

TimeSpan defaultTimeout = TimeSpan.FromSeconds(100.0);
TimeSpan maxTimeout = TimeSpan.FromMilliseconds((double) int.MaxValue);
TimeSpan infiniteTimeout = TimeSpan.FromMilliseconds(-1.0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM