简体   繁体   English

如何解决“操作已超时”错误

[英]How to Resolve “The operation has timed out” error

I have a problem that while downloading the data it shows the error "The Operation has timed out".我有一个问题,即在下载数据时显示错误“操作已超时”。

What can i do to resolve this error?我能做些什么来解决这个错误? I am using Win forms(C#) here is my code please check it and give suggestions.我正在使用 Win forms(C#) 这是我的代码,请检查并提出建议。 Where should i change the code please help me...我应该在哪里更改代码,请帮助我...

  public void ProcessData()
        {


            try
            {
            string MessageTitle = "";
            int pages = Convert.ToInt32(txtPages.Text);

            for (int k = Count; k <= pages; k++)
            {

                string url = "http://www.yellowpages.com/" +StateName.ToLower()+ "/" + CategoryName + "?g=" + StateName + "&page=" + k + "&q=" + CategoryName + "";//txtYP.Text + k;
                System.Net.HttpWebRequest httpRequest;
                System.Net.HttpWebResponse httpResponse;
                System.IO.StreamReader SReader;
                string html;
                httpRequest = (System.Net.HttpWebRequest)(System.Net.HttpWebRequest.Create(url));
                httpRequest.Method = "GET";
                httpResponse = (System.Net.HttpWebResponse)(httpRequest.GetResponse());
                SReader = new StreamReader(httpResponse.GetResponseStream());
                html = SReader.ReadToEnd();
                string strDummy = html;
                httpResponse.Close();

How long is it before the request times out?在请求超时之前多长时间? Are you able to navigate to the url from a web browser?您是否能够从 web 浏览器导航到 url?

Set HttpWebRequest.ReadWriteTimeout property on HttpWebRequest to a much higher value than what it is currently.HttpWebRequest.ReadWriteTimeout上的 HttpWebRequest.ReadWriteTimeout 属性设置为比当前值高得多的值。 The default value is 5 minutes.默认值为 5 分钟。 Not sure why it should take more than 5 minutes.不知道为什么需要超过 5 分钟。

Instead of blocking on the getresponse, you could as well use async callbacks ( BeginGetResponse / EndGetResponse ).除了阻塞 getresponse,您还可以使用异步回调( BeginGetResponse / EndGetResponse )。

EDIT编辑

<system.diagnostics>
  <trace autoflush="true" />
  <sources>
    <source name="System.Net">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.HttpListener">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Sockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Cache">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add
      name="System.Net"
      type="System.Diagnostics.TextWriterTraceListener"
      initializeData="trace.log"
      traceOutputOptions = "ProcessId, DateTime"
            />
  </sharedListeners>
  <switches>
    <add name="System.Net"
         value="Verbose" />
    <add name="System.Net.Sockets"
         value="Verbose" />
    <add name="System.Net.Cache"
         value="Verbose" />
    <add name="System.Net.HttpListener"
         value="Verbose" />
  </switches>
</system.diagnostics>  

Add this section inside configuration section in the app.config of your application.在应用程序的 app.config 中的configuration部分中添加此部分。 After adding the above, rebuild the solution and run it.添加上述内容后,重建解决方案并运行它。 Look at the trace.log written in the bin directory of your application for more details.查看应用程序的 bin 目录中写入的 trace.log 以了解更多详细信息。

add this to your code:将此添加到您的代码中:

httpRequest.Timeout = 3600000; httpRequest.Timeout = 3600000;

this will increase the request timeout to one hour.这会将请求超时增加到一小时。

I have face same issue while executing my console application on server.在服务器上执行我的控制台应用程序时,我遇到了同样的问题。 Below solution work for me:以下解决方案对我有用:

Uncheck the automatic configuration script under Lan settings in internet option and check the automatic detect settings.在 Internet 选项中取消选中 LAN 设置下的自动配置脚本并检查自动检测设置。 It resolve my problem for operation time out error.它解决了我的操作超时错误问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM