使用HttpWebRequest进行HTTP调用,我发现它没有Timeout属性?
例如:
HttpWebRequest request = WebRequest.Create(aWebUrl) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = "GET";
由于
答案 0 :(得分:2)
我想你可以看看James Montemagno's plugin
有一个Utils可以帮助您检查异步调用是否超时。我认为您可以将它与Web请求一起使用。
namespace MvvmHelpers
{
/// <summary>
/// Extension Utils
/// </summary>
public static class Utils
{
/// <summary>
/// Task extension to add a timeout.
/// </summary>
/// <returns>The task with timeout.</returns>
/// <param name="task">Task.</param>
/// <param name="timeoutInMilliseconds">Timeout duration in Milliseconds.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public async static Task<T> WithTimeout<T>(this Task<T> task, int timeoutInMilliseconds)
{
var retTask = await Task.WhenAny(task, Task.Delay(timeoutInMilliseconds))
.ConfigureAwait(false);
if (retTask is Task<T>)
return task.Result;
return default(T);
}
/// <summary>
/// Task extension to add a timeout.
/// </summary>
/// <returns>The task with timeout.</returns>
/// <param name="task">Task.</param>
/// <param name="timeout">Timeout Duration.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public static Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout) =>
WithTimeout(task, (int)timeout.TotalMilliseconds);
}
}
您可以在此视频Channel 9 MVVM helper
中找到相关说明否则你可以使用具有&#34;超时&#34;的HttpClient。属性