Exception: The operation has timed-out.

Discussion in 'ASP .Net' started by news.microsoft.com, Oct 28, 2005.

  1. I experienced the following exception occasionally when the program
    downloads the image from the Web site. Who can give me some solution for it?
    Thanks in advance.


    ----------
    Exception:
    Message: The operation has timed-out.
    Source: System
    TargetSite: Int32 Read(Byte[], Int32, Int32)
    StackTrace:
    at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32
    size)
    at RTC7.ImageSystem.ViewPictureInRecord.Copy(Stream stream, String
    filename)
    at RTC7.ImageSystem.ViewPictureInRecord.LoadImageFromServer(String sUrl),


    //-------------
    // Codes:
    public string LoadImageFromServer(string sUrl)
    {
    bool isAborted = false;
    string strURL_ = sUrl; //this.GetUrlOfPicture();
    string strLocalTmpFile = "";
    Stream stream = null;
    WebRequest webreq = null;
    WebResponse webres = null;
    try
    {
    Uri uri = new Uri(strURL_);

    webreq = WebRequest.CreateDefault(uri);

    webreq.Timeout = 60000;
    webres = webreq.GetResponse();

    if(webres != null)
    stream = webres.GetResponseStream();
    else
    return null;
    //...
    }
    catch(Exception ex)
    {
    // ...
    }
    finally
    {
    if(webres != null)
    webres.Close();
    if(stream != null)
    stream.Close();
    }
    return strLocalTmpFile;
    }

    public bool Copy(Stream stream, string filename)
    {
    const int conBufferLeng = 20000;
    FileStream fstream= null;
    byte[] buffer = new byte[conBufferLeng];
    int iByteCountInBuffer = conBufferLeng;
    bool blnOkay = false;
    try
    {
    if(stream != null)
    {
    if(File.Exists(filename) == false)
    fstream = File.Create(filename);
    else
    fstream = File.OpenWrite(filename);
    if(fstream != null && fstream.CanWrite == true)
    {
    if(stream.CanSeek == true)
    stream.Position = 0;
    do
    {
    lock(this.syncObject)
    iByteCountInBuffer = stream.Read(buffer,0,conBufferLeng);
    fstream.Write(buffer,0,iByteCountInBuffer);
    }
    while(iByteCountInBuffer > 0); //conBufferLeng);
    blnOkay = true;
    } // If destination is okay.
    } // If source is okay.
    }
    finally
    {
    if(fstream != null)
    fstream.Close();
    buffer = null;
    }
    return blnOkay;
    }
     
    news.microsoft.com, Oct 28, 2005
    #1
    1. Advertising

  2. Try again?
    Show the user a message?

    You have an external dependency. You are attempting to download an image
    from a location you have no control over. It is a network resource. If the
    operation times out, the response took longer than the 60 seconds you
    instructed the WebRequest to wait for. Perhaps the image is way large.
    Perhaps the server quit responding. Perhaps a packet was dropped. Maybe some
    guy in a car ran into the wrong telephone pole. Who knows? It's an external
    dependency. You have to handle this sort of contingency. How you handle it
    is your requirement to decide.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    A watched clock never boils.

    "news.microsoft.com" <> wrote in message
    news:...
    >I experienced the following exception occasionally when the program
    > downloads the image from the Web site. Who can give me some solution for
    > it?
    > Thanks in advance.
    >
    >
    > ----------
    > Exception:
    > Message: The operation has timed-out.
    > Source: System
    > TargetSite: Int32 Read(Byte[], Int32, Int32)
    > StackTrace:
    > at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32
    > size)
    > at RTC7.ImageSystem.ViewPictureInRecord.Copy(Stream stream, String
    > filename)
    > at RTC7.ImageSystem.ViewPictureInRecord.LoadImageFromServer(String
    > sUrl),
    >
    >
    > //-------------
    > // Codes:
    > public string LoadImageFromServer(string sUrl)
    > {
    > bool isAborted = false;
    > string strURL_ = sUrl; //this.GetUrlOfPicture();
    > string strLocalTmpFile = "";
    > Stream stream = null;
    > WebRequest webreq = null;
    > WebResponse webres = null;
    > try
    > {
    > Uri uri = new Uri(strURL_);
    >
    > webreq = WebRequest.CreateDefault(uri);
    >
    > webreq.Timeout = 60000;
    > webres = webreq.GetResponse();
    >
    > if(webres != null)
    > stream = webres.GetResponseStream();
    > else
    > return null;
    > //...
    > }
    > catch(Exception ex)
    > {
    > // ...
    > }
    > finally
    > {
    > if(webres != null)
    > webres.Close();
    > if(stream != null)
    > stream.Close();
    > }
    > return strLocalTmpFile;
    > }
    >
    > public bool Copy(Stream stream, string filename)
    > {
    > const int conBufferLeng = 20000;
    > FileStream fstream= null;
    > byte[] buffer = new byte[conBufferLeng];
    > int iByteCountInBuffer = conBufferLeng;
    > bool blnOkay = false;
    > try
    > {
    > if(stream != null)
    > {
    > if(File.Exists(filename) == false)
    > fstream = File.Create(filename);
    > else
    > fstream = File.OpenWrite(filename);
    > if(fstream != null && fstream.CanWrite == true)
    > {
    > if(stream.CanSeek == true)
    > stream.Position = 0;
    > do
    > {
    > lock(this.syncObject)
    > iByteCountInBuffer = stream.Read(buffer,0,conBufferLeng);
    > fstream.Write(buffer,0,iByteCountInBuffer);
    > }
    > while(iByteCountInBuffer > 0); //conBufferLeng);
    > blnOkay = true;
    > } // If destination is okay.
    > } // If source is okay.
    > }
    > finally
    > {
    > if(fstream != null)
    > fstream.Close();
    > buffer = null;
    > }
    > return blnOkay;
    > }
    >
    >
    >
    >
     
    Kevin Spencer, Oct 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    0
    Views:
    5,392
    Steve
    Jul 1, 2003
  2. Matt
    Replies:
    0
    Views:
    4,049
  3. =?Utf-8?B?S2h1cnJhbSBJc2hmYXEgUmF0aG9y?=

    Operation has timed out in ASP.Net Application

    =?Utf-8?B?S2h1cnJhbSBJc2hmYXEgUmF0aG9y?=, Sep 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    4,288
    =?Utf-8?B?S2h1cnJhbSBJc2hmYXEgUmF0aG9y?=
    Sep 2, 2004
  4. Joel Zhou

    "The operation has timed-out." exception on WinXP

    Joel Zhou, Jul 17, 2003, in forum: ASP .Net Web Services
    Replies:
    20
    Views:
    961
    Ramana
    Oct 1, 2003
  5. Alex Litvak

    random "The operation has timed-out." exception in vb.net

    Alex Litvak, Oct 8, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    314
    Sachin Surana
    Feb 2, 2005
Loading...

Share This Page