B
Brian
I'm having intermittent trouble with a call to the Read method of
the HttpWebResponse object. I get an ArumentOutOfRangeException
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.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: count
at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst,
Int32 dstOffset, Int32 count)
at System.Net.ConnectStream.FillFromBufferedData(Byte[] buffer,
Int32& offset, Int32& size)
at System.Net.ConnectStream.FillFromBufferedData(NestedSingleAsyncResult
castedAsyncResult, Byte[] buffer, Int32& offset, Int32& size)
at System.Net.ConnectStream.InternalBeginRead(Int32 bytesToRead,
NestedSingleAsyncResult castedAsyncResult, Boolean fromCallback)
at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[]
buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
state)
at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32
size)
at BIZv1.Helpers.HttpHelper.FileTransfer(String urlpath, String
destination)
at BIZv1.BLL.MoveFilesFromBIZBLL.SaveFileFromBIZ()
public bool FileTransfer(string urlpath, string destination)
{
Stream istream = null;
FileStream ostream = null;
WebRequest wreq = WebRequest.Create( urlpath );
HttpWebResponse response;
byte[] buffer = new byte[this.m_bufferSize]; // 1024
int size = m_bufferSize; //1024
try
{
response = (HttpWebResponse) wreq.GetResponse();
istream = response.GetResponseStream();
long contentLength = Convert.ToInt64(response.ContentLength);
this.m_results += DateTime.Now.ToString("HH:mm:ss.ffffff") + ":
Downloading " + contentLength.ToString() + " bytes./n";
// Always overwrite downloaded file...
ostream = new FileStream(destination, FileMode.Create,
FileAccess.Write);
int maxCounter = 2000;
int counter = 0;
// Read and write data
while (true)
{
size = istream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
ostream.Write(buffer, 0, size);
}
else
{
break;
}
counter++;
if(counter >= maxCounter)
{
counter = 0;
System.Threading.Thread.Sleep(20);
}
}
}
catch(System.ArgumentOutOfRangeException 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 ArumentOutOfRangeException
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.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: count
at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst,
Int32 dstOffset, Int32 count)
at System.Net.ConnectStream.FillFromBufferedData(Byte[] buffer,
Int32& offset, Int32& size)
at System.Net.ConnectStream.FillFromBufferedData(NestedSingleAsyncResult
castedAsyncResult, Byte[] buffer, Int32& offset, Int32& size)
at System.Net.ConnectStream.InternalBeginRead(Int32 bytesToRead,
NestedSingleAsyncResult castedAsyncResult, Boolean fromCallback)
at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[]
buffer, Int32 offset, Int32 size, AsyncCallback callback, Object
state)
at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset,
Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32
size)
at BIZv1.Helpers.HttpHelper.FileTransfer(String urlpath, String
destination)
at BIZv1.BLL.MoveFilesFromBIZBLL.SaveFileFromBIZ()
public bool FileTransfer(string urlpath, string destination)
{
Stream istream = null;
FileStream ostream = null;
WebRequest wreq = WebRequest.Create( urlpath );
HttpWebResponse response;
byte[] buffer = new byte[this.m_bufferSize]; // 1024
int size = m_bufferSize; //1024
try
{
response = (HttpWebResponse) wreq.GetResponse();
istream = response.GetResponseStream();
long contentLength = Convert.ToInt64(response.ContentLength);
this.m_results += DateTime.Now.ToString("HH:mm:ss.ffffff") + ":
Downloading " + contentLength.ToString() + " bytes./n";
// Always overwrite downloaded file...
ostream = new FileStream(destination, FileMode.Create,
FileAccess.Write);
int maxCounter = 2000;
int counter = 0;
// Read and write data
while (true)
{
size = istream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
ostream.Write(buffer, 0, size);
}
else
{
break;
}
counter++;
if(counter >= maxCounter)
{
counter = 0;
System.Threading.Thread.Sleep(20);
}
}
}
catch(System.ArgumentOutOfRangeException 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...