Download timeout error

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

I experienced the following exception occasionally, which happens when the
program downloads the image data from the Web site. The program first
downloads some bytes successfully, and then waits for 5 minutes. At last, it
reports "The operation has timed-out". I must declare that this problem
doesn't happen in the most cases. The .NET Platform of the client program
and the ASP.NET program are all V1.1.

Who can give me some solutions 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.Image.ViewPictureInRecord.Copy(Stream stream, String filename)
at RTC7.Image.ViewPictureInRecord.LoadImageFromServer(String sUrl),


//-------------
// Codes of the client program to download the image data:
public string LoadImageFromServer(string sUrl)
{
bool isAborted = false;
string strURL_ = sUrl;
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
{
iByteCountInBuffer = stream.Read(buffer,0,conBufferLeng);
fstream.Write(buffer,0,iByteCountInBuffer);
}
while(iByteCountInBuffer > 0);
blnOkay = true;
} // If destination is okay.
} // If source is okay.
}
finally
{
if(fstream != null)
fstream.Close();
buffer = null;
}
return blnOkay;
}


/// <summary>
/// The codes of .NET ASP page to download the image data.
/// </summary>
public class GetPicturePage: System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
const string CSqlSeletImageInf = "select img from IMAGE where sys_ID =
{0}";
try
{
string sID = Request["id"];
string SQLComm = string.Format(CSqlSeletImageInf, sID);

//-------------------------------
// Retrieve the image data from the database.
byte[] img = ImgTool.GetImage(SQLComm);

//--If the img is null, return directly.
if (img == null)
{
return;
}

// In the most cases, the size of the img is 200KB.
// Sometimes, it's 2~3 MB.
// Only in a few cases, it's 20 MB.
Response.ContentType = "image/jpeg";
Response.BinaryWrite(img);
return;
//...
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
Response.End();
//...
}
}

//...
}
 
It is possible that the website itself is actually timing out on the
request. In fact, it sounds like that is exactly the error message you are
receiving. If you own the website, you can do some performance tuning to
alleviate this issue. If it is not your website, then I think you are stuck
with the issue. Any time you are connecting to a website there is a chance
your request is going to time out.


news.microsoft.com said:
I experienced the following exception occasionally, which happens when the
program downloads the image data from the Web site. The program first
downloads some bytes successfully, and then waits for 5 minutes. At last, it
reports "The operation has timed-out". I must declare that this problem
doesn't happen in the most cases. The .NET Platform of the client program
and the ASP.NET program are all V1.1.

Who can give me some solutions 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.Image.ViewPictureInRecord.Copy(Stream stream, String filename)
at RTC7.Image.ViewPictureInRecord.LoadImageFromServer(String sUrl),


//-------------
// Codes of the client program to download the image data:
public string LoadImageFromServer(string sUrl)
{
bool isAborted = false;
string strURL_ = sUrl;
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
{
iByteCountInBuffer = stream.Read(buffer,0,conBufferLeng);
fstream.Write(buffer,0,iByteCountInBuffer);
}
while(iByteCountInBuffer > 0);
blnOkay = true;
} // If destination is okay.
} // If source is okay.
}
finally
{
if(fstream != null)
fstream.Close();
buffer = null;
}
return blnOkay;
}


/// <summary>
/// The codes of .NET ASP page to download the image data.
/// </summary>
public class GetPicturePage: System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
const string CSqlSeletImageInf = "select img from IMAGE where sys_ID =
{0}";
try
{
string sID = Request["id"];
string SQLComm = string.Format(CSqlSeletImageInf, sID);

//-------------------------------
// Retrieve the image data from the database.
byte[] img = ImgTool.GetImage(SQLComm);

//--If the img is null, return directly.
if (img == null)
{
return;
}

// In the most cases, the size of the img is 200KB.
// Sometimes, it's 2~3 MB.
// Only in a few cases, it's 20 MB.
Response.ContentType = "image/jpeg";
Response.BinaryWrite(img);
return;
//...
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
Response.End();
//...
}
}

//...
}
 
Back
Top