Victor Athoti.
Victor Athoti.

Reputation: 831

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();

Upvotes: 3

Views: 50142

Answers (3)

Mitali Gupta
Mitali Gupta

Reputation: 1

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 .

Upvotes: 0

Arrabi
Arrabi

Reputation: 3768

add this to your code :

httpRequest.Timeout = 3600000;

this will increase the request timeout to one hour.

Upvotes: 0

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4711

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.

Upvotes: 1

Related Questions