Timeout behaviour in HttpWebRequest.GetResponse() vs GetResponseAsync()

前端 未结 2 1974
北海茫月
北海茫月 2020-12-18 20:49

When I try the following code:

var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = 3; // a small value

var response = request.GetRes         


        
相关标签:
2条回答
  • 2020-12-18 21:54

    Timeout does not apply to asynchronous HttpWebRequest requests. To quote the docs:

    The Timeout property has no effect on asynchronous requests

    I recommend you use HttpClient instead, which was designed with asynchronous requests in mind.

    0 讨论(0)
  • 2020-12-18 21:54

    Follow a solution to solve the problem.

    await Task.Run(() => { 
      var varHttpResponse = varWebRequest.GetResponse(); 
    });
    
    0 讨论(0)
提交回复
热议问题