Problem uploading files with HttpWebRequest

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello folks,

I'm having a problem uploading files. The code that is responsible for this
works almost all the time, but fails when trying to upload files that
approach a megabyte in size or more, from a server that has a slow internet
connection. The exceptions I get are:

System.Net.WebException: The underlying connection was closed: The request
was canceled.
at System.Net.HttpWebRequest.GetResponse()

System.Net.WebException: The operation has timed-out.
at System.Net.HttpWebRequest.GetResponse()

Some pertinent facts:
- I also get this error when using WebClient.UploadFile.
- Both servers are using the 1.1 version of the .net framework.
- The sender is running windows server 2003.
- The receiver is running windows 2000 server.
- Uses https.
- No anonymous access, basic & integrated windows authentication.
- The receiving aspx page is never executed.
- The weblogs for the failed cases show a hit without credentials resulting
in a 401, followed just less than 2 minutes later with a hit without
credentials resulting in a 500.
- Good credentials are being supplied ( the code works for smaller files, &
faster connections ).
- Specifying SendChunked does not affect the outcome.

The code in question:

//----------
NetworkCredential cred = new NetworkCredential( sUser, sPassword, sDomain );
Uri uri = new Uri( sURL );
HttpWebRequest req = ( HttpWebRequest )WebRequest.Create( uri );

req.Credentials = cred;
req.ContentType = "multipart/form-data; boundary=" + sBoundary;
req.Method = "POST";

using ( FileStream stmFile = new FileStream( sFileName, FileMode.Open,
FileAccess.Read ) ) {
long lContentLength = bufPostHeader.Length + stmFile.Length +
bufBoundary.Length;

req.ContentLength = lContentLength;

using ( Stream stmReq = req.GetRequestStream() ) {
// Write out our post header
stmReq.Write( bufPostHeader, 0, bufPostHeader.Length );

// Write out the file contents
byte[] buf = new Byte[ 4096 ];
int iBytesRead = 0;
while ( ( iBytesRead = stmFile.Read( buf, 0, buf.Length ) ) != 0 ) {
stmReq.Write( buf, 0, iBytesRead );
}

// Write out the trailing boundary
stmReq.Write( bufBoundary, 0, bufBoundary.Length );
}
}

string sRsp;
using ( WebResponse rsp = req.GetResponse() ) {
using ( Stream stmRsp = rsp.GetResponseStream() ) {
StreamReader sr = new StreamReader( stmRsp );
sRsp = sr.ReadToEnd();
}
}
//----------

This problem has me stumped. I guess my next option is to use the
asynchronous calls & implement my own timeout mechanism, but I was really
hoping that someone, somewhere had encountered this issue & could point me to
a solution so I don't have to continue sinking time coding in the dark.

Thanks for any help....
 
AnCap said:
Hello folks,

I'm having a problem uploading files. The code that is responsible
for this works almost all the time, but fails when trying to upload
files that approach a megabyte in size or more, from a server that
has a slow internet connection. The exceptions I get are:

System.Net.WebException: The underlying connection was closed: The
request was canceled.
at System.Net.HttpWebRequest.GetResponse()

System.Net.WebException: The operation has timed-out.
at System.Net.HttpWebRequest.GetResponse()

Some pertinent facts:
- I also get this error when using WebClient.UploadFile.
- Both servers are using the 1.1 version of the .net framework.
- The sender is running windows server 2003.
- The receiver is running windows 2000 server.
- Uses https.
- No anonymous access, basic & integrated windows authentication.
- The receiving aspx page is never executed.
- The weblogs for the failed cases show a hit without credentials
resulting in a 401, followed just less than 2 minutes later with a
hit without credentials resulting in a 500.
- Good credentials are being supplied ( the code works for smaller
files, & faster connections ).
- Specifying SendChunked does not affect the outcome.

The code in question:

//----------
NetworkCredential cred = new NetworkCredential( sUser, sPassword,
sDomain ); Uri uri = new Uri( sURL );
HttpWebRequest req = ( HttpWebRequest )WebRequest.Create( uri );

req.Credentials = cred;
req.ContentType = "multipart/form-data; boundary=" + sBoundary;
req.Method = "POST";

using ( FileStream stmFile = new FileStream( sFileName,
FileMode.Open, FileAccess.Read ) ) {
long lContentLength = bufPostHeader.Length + stmFile.Length +
bufBoundary.Length;

req.ContentLength = lContentLength;

using ( Stream stmReq = req.GetRequestStream() ) {
// Write out our post header
stmReq.Write( bufPostHeader, 0, bufPostHeader.Length );

// Write out the file contents
byte[] buf = new Byte[ 4096 ];
int iBytesRead = 0;
while ( ( iBytesRead = stmFile.Read( buf, 0, buf.Length ) ) != 0 ) {
stmReq.Write( buf, 0, iBytesRead );
}

// Write out the trailing boundary
stmReq.Write( bufBoundary, 0, bufBoundary.Length );
}
}

string sRsp;
using ( WebResponse rsp = req.GetResponse() ) {
using ( Stream stmRsp = rsp.GetResponseStream() ) {
StreamReader sr = new StreamReader( stmRsp );
sRsp = sr.ReadToEnd();
}
}
//----------

This problem has me stumped. I guess my next option is to use the
asynchronous calls & implement my own timeout mechanism, but I was
really hoping that someone, somewhere had encountered this issue &
could point me to a solution so I don't have to continue sinking time
coding in the dark.

Thanks for any help....

I didn't spot any glaring error in your code... and since WebClient
fails as well, there's a strong indication that it's more of an
infrastructure issue. Did you try to increase ReadWriteTimeout? Did you
test a file upload sent by a web browser?

Cheers,
 
Thanks, Joerg.

I had overlooked the ReadWriteTimeout. Extending both it and the Timeout
period were required to overcome this hurdle.

Thanks again!
 
Back
Top