We have a .NET 2.0 WinForms application that should upload files to the IIS6 server via WebDav. From time to time, we receive complaints from the remote office that they receive one of the following error messages
- The connected connection was closed: an unexpected error occurred while sending.
- The connected connection was closed: An unexpected error occurred while receiving.
This only happens with large files (~ 20 MB plus). I tested it with a 40 MB file from my home computer and tried to put "Sleep in a loop to simulate a slow connection, so I suspect this is due to network problems at the end ... but
- IT in a remote office does not help
- I would like to rule out the possibility that my code is to blame.
So - someone can identify any errors or suggest some workarounds that can "bulletproof" the code against this problem. Thanks for any help. The following version of the code is subtracted:
public bool UploadFile(string localFile, string uploadUrl)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
try
{
req.Method = "PUT";
req.AllowWriteStreamBuffering = true;
req.UseDefaultCredentials = Program.WebService.UseDefaultCredentials;
req.Credentials = Program.WebService.Credentials;
req.SendChunked = false;
req.KeepAlive = true;
Stream reqStream = req.GetRequestStream();
FileStream rdr = new FileStream(localFile, FileMode.Open, FileAccess.Read);
byte[] inData = new byte[4096];
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
}
reqStream.Close();
rdr.Close();
System.Net.HttpWebResponse response = (HttpWebResponse)req.GetResponse();
if (response.StatusCode != HttpStatusCode.OK && response.StatusCode!=HttpStatusCode.Created)
{
MessageBox.Show("Couldn't upload file");
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
return true;
}
source
share