I'm having intermittent trouble with a call to the Read method of
the HttpWebResponse object. I get an ArumentOutOfRan geException
claiming that deep down inside of the Read method, the count parameter
of the BlockCopy method is showing up as a negative number. Don't
know where the negative number is coming from since I've passed the
value of 1024 to the Read and Write methods. I've even tested the
value of buffer.Length and size at the time of the exception and they
are both 1024.
The exception and the source code are listed below:
System.Argument OutOfRangeExcep tion: Non-negative number required.
Parameter name: count
at System.Buffer.B lockCopy(Array src, Int32 srcOffset, Array dst,
Int32 dstOffset, Int32 count)
at System.Net.Conn ectStream.FillF romBufferedData (Byte[] buffer,
Int32& offset, Int32& size)
at System.Net.Conn ectStream.FillF romBufferedData (NestedSingleAs yncResult
castedAsyncResu lt, Byte[] buffer, Int32& offset, Int32& size)
at System.Net.Conn ectStream.Inter nalBeginRead(In t32 bytesToRead,
NestedSingleAsy ncResult castedAsyncResu lt, Boolean fromCallback)
at System.Net.Conn ectStream.Begin ReadWithoutVali dation(Byte[]
buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
state)
at System.Net.Conn ectStream.Begin Read(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.Conn ectStream.Read( Byte[] buffer, Int32 offset, Int32
size)
at BIZv1.Helpers.H ttpHelper.FileT ransfer(String urlpath, String
destination)
at BIZv1.BLL.MoveF ilesFromBIZBLL. SaveFileFromBIZ ()
public bool FileTransfer(st ring urlpath, string destination)
{
Stream istream = null;
FileStream ostream = null;
WebRequest wreq = WebRequest.Crea te( urlpath );
HttpWebResponse response;
byte[] buffer = new byte[this.m_bufferSi ze]; // 1024
int size = m_bufferSize; //1024
try
{
response = (HttpWebRespons e) wreq.GetRespons e();
istream = response.GetRes ponseStream();
long contentLength = Convert.ToInt64 (response.Conte ntLength);
this.m_results += DateTime.Now.To String("HH:mm:s s.ffffff") + ":
Downloading " + contentLength.T oString() + " bytes./n";
// Always overwrite downloaded file...
ostream = new FileStream(dest ination, FileMode.Create ,
FileAccess.Writ e);
int maxCounter = 2000;
int counter = 0;
// Read and write data
while (true)
{
size = istream.Read(bu ffer, 0, buffer.Length);
if (size > 0)
{
ostream.Write(b uffer, 0, size);
}
else
{
break;
}
counter++;
if(counter >= maxCounter)
{
counter = 0;
System.Threadin g.Thread.Sleep( 20);
}
}
}
catch(System.Ar gumentOutOfRang eException e1)
{
string errorMessage = "Problems with getting\n" + urlpath + " to
save to \n" + destination + "\n";
[extraneous exception handling code deleted]
Thanks in advance for anyone's help...
the HttpWebResponse object. I get an ArumentOutOfRan geException
claiming that deep down inside of the Read method, the count parameter
of the BlockCopy method is showing up as a negative number. Don't
know where the negative number is coming from since I've passed the
value of 1024 to the Read and Write methods. I've even tested the
value of buffer.Length and size at the time of the exception and they
are both 1024.
The exception and the source code are listed below:
System.Argument OutOfRangeExcep tion: Non-negative number required.
Parameter name: count
at System.Buffer.B lockCopy(Array src, Int32 srcOffset, Array dst,
Int32 dstOffset, Int32 count)
at System.Net.Conn ectStream.FillF romBufferedData (Byte[] buffer,
Int32& offset, Int32& size)
at System.Net.Conn ectStream.FillF romBufferedData (NestedSingleAs yncResult
castedAsyncResu lt, Byte[] buffer, Int32& offset, Int32& size)
at System.Net.Conn ectStream.Inter nalBeginRead(In t32 bytesToRead,
NestedSingleAsy ncResult castedAsyncResu lt, Boolean fromCallback)
at System.Net.Conn ectStream.Begin ReadWithoutVali dation(Byte[]
buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
state)
at System.Net.Conn ectStream.Begin Read(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.Conn ectStream.Read( Byte[] buffer, Int32 offset, Int32
size)
at BIZv1.Helpers.H ttpHelper.FileT ransfer(String urlpath, String
destination)
at BIZv1.BLL.MoveF ilesFromBIZBLL. SaveFileFromBIZ ()
public bool FileTransfer(st ring urlpath, string destination)
{
Stream istream = null;
FileStream ostream = null;
WebRequest wreq = WebRequest.Crea te( urlpath );
HttpWebResponse response;
byte[] buffer = new byte[this.m_bufferSi ze]; // 1024
int size = m_bufferSize; //1024
try
{
response = (HttpWebRespons e) wreq.GetRespons e();
istream = response.GetRes ponseStream();
long contentLength = Convert.ToInt64 (response.Conte ntLength);
this.m_results += DateTime.Now.To String("HH:mm:s s.ffffff") + ":
Downloading " + contentLength.T oString() + " bytes./n";
// Always overwrite downloaded file...
ostream = new FileStream(dest ination, FileMode.Create ,
FileAccess.Writ e);
int maxCounter = 2000;
int counter = 0;
// Read and write data
while (true)
{
size = istream.Read(bu ffer, 0, buffer.Length);
if (size > 0)
{
ostream.Write(b uffer, 0, size);
}
else
{
break;
}
counter++;
if(counter >= maxCounter)
{
counter = 0;
System.Threadin g.Thread.Sleep( 20);
}
}
}
catch(System.Ar gumentOutOfRang eException e1)
{
string errorMessage = "Problems with getting\n" + urlpath + " to
save to \n" + destination + "\n";
[extraneous exception handling code deleted]
Thanks in advance for anyone's help...
Comment