WebRequest.Create-操作已超时 [英] WebRequest.Create - The operation has timed out

查看:277
本文介绍了WebRequest.Create-操作已超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在自己的网站上抓取几个页面,但是我的实时环境中出现了webException(操作已超时")超时,但不是在我的测试环境中.超时不会在同一页面上发生两次,而是随机发生,并且经常在某些请求之后发生.第一次超时后,超时的频率增加.

I'm trying to crawl a couple of pages on my own site, but I'm getting a time-out webException("The operation has timed out") on my live environment but not on my test environment. The time-out does not occur on the same page twice, but randomly and often after some requests. After the first time-out, the frequency of the time-outs rises.

关于测试环境的requestUristring: http://localhost/Opgaver/Flytning/Haarde-hvidevarer/Bortkoersel -amerikaner-koeleskab-paa.aspx

The requestUristring on test enviroment: http://localhost/Opgaver/Flytning/Haarde-hvidevarer/Bortkoersel-amerikaner-koeleskab-paa.aspx

实时环境中的requestUristring: http://www.servicebyen.dk/Opgaver /Flytning/Haarde-hvidevarer/Bortkoersel-amerikaner-koeleskab-paa.aspx

The requestUristring on live enviroment: http://www.servicebyen.dk/Opgaver/Flytning/Haarde-hvidevarer/Bortkoersel-amerikaner-koeleskab-paa.aspx

var webRequest = (HttpWebRequest)WebRequest.Create(requestUriString);
webRequest.KeepAlive = false;
webRequest.Timeout = 3 * 30 * 1000;
webRequest.PreAuthenticate = false;

using (WebResponse webResponse = webRequest.GetResponse()) //ERROR OCCURS HERE
{

    using (Stream responseStream = webResponse.GetResponseStream())
    {
         if (responseStream != null)
         {
              using (var reader = new StreamReader(responseStream))
              {
                   string readToEnd = reader.ReadToEnd();
                   .....          
              }

         }                
     }
 }

推荐答案

尝试一下(但仅适用于您的测试环境).

Try this (but only for your test environment).

保持活动:如果对Internet资源的请求应包含值为

Keep-alive: true if the request to the Internet resource should contain a Connection HTTP header with the value

webRequest.KeepAlive = true;
webRequest.Timeout = 95 * 95 * 100000;

webRequest.KeepAlive = true;
webRequest.Timeout = 95 * 95 * 100000;

这篇关于WebRequest.Create-操作已超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆