【问题标题】:Why I can not get Timeout on HttpWebRequest? (HttpWebRequest Timeout nightmare)为什么我无法在 HttpWebRequest 上获得超时? (HttpWebRequest 超时噩梦)
【发布时间】:2012-09-11 14:07:45
【问题描述】:

我在 Quartz.Net 框架中使用它。每个 Job(thread) 只有一个 web request

这是检查清单:

  • 所有一次性对象都用于使用块!

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
                str = reader.ReadToEnd();
    
  • ServicePointManager.Expect100Continue = false;
  • ServicePointManager.DefaultConnectionLimit = 1000;
  • ServicePointManager.MaxServicePoints = 1000;
  • request.KeepAlive=false
  • request.Timeout = 5000;
  • request.ReadWriteTimeout = 5000;
  • request.ServicePoint.ConnectionLeaseTimeout = 5000;
  • request.ServicePoint.MaxIdleTime = 5000;

我正在使用秒表来测量网络请求时间。这些是日志:

异常Web请求时间:16333 ms

异常Web请求时间:8350毫秒

异常Web请求时间:9846毫秒

异常Web请求时间:7545毫秒

异常Web请求时间:6662 ms

异常Web请求时间:18332 ms

有什么想法吗?

PS:本题是Timeout supported Multithreaded Web Request的总结版

【问题讨论】:

    标签: httpwebrequest timeout


    【解决方案1】:

    我从这个问题中学到的唯一教训:如果您在多个线程中使用 HttpWebRequest,请不要同步使用它!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2018-02-13
      相关资源
      最近更新 更多