Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2005

Tip: Looking for answers? Try searching our database.

Problem uploading files with HttpWebRequest

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AnCap - 01 Dec 2005 14:15 GMT
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....
Joerg Jooss - 01 Dec 2005 21:37 GMT
> Hello folks,
>
[quoted text clipped - 75 lines]
>
> 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,
Signature

http://www.joergjooss.de
mailto:news-reply@joergjooss.de

AnCap - 02 Dec 2005 18:39 GMT
Thanks, Joerg.

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

Thanks again!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.