简体   繁体   中英

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. 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?

Set HttpWebRequest.ReadWriteTimeout property on HttpWebRequest to a much higher value than what it is currently. The default value is 5 minutes. Not sure why it should take more than 5 minutes.

Instead of blocking on the getresponse, you could as well use async callbacks ( 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. 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.

add this to your code:

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. It resolve my problem for operation time out error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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