Fast HTTPS Upload Code Needed

Posted on 2009-04-16
Last Modified: 2013-12-17
Hi all,

I need some fast HTTPS upload Code.

Here's the code I'm currently using.  It's pretty slow, although my download code screams.  While reading the file and writing to the stream is fast, actually sending the data using request.GetResponse is horribly slow.  I'm open to any ideas...

    Public Function UploadFile(ByVal filePath As String, _
                                         ByVal address As String, _
                                         ByRef pb As System.Windows.Forms.ToolStripProgressBar) _
                                         As Boolean

        'Get the length of the selected file
        Dim fi As IO.FileInfo = My.Computer.FileSystem.GetFileInfo(filePath)
        Dim fileLength = fi.Length

        'Create the request
        Dim request As HttpWebRequest = _
            DirectCast(HttpWebRequest.Create(address), HttpWebRequest)

        'Configure request
        request.Credentials = New System.Net.NetworkCredential("user", "password")
        request.Method = WebRequestMethods.Http.Put
        request.ContentLength = fileLength.ToString()

        'This is required for our WebDav server (will error without it)
        request.SendChunked = True
        request.Headers.Add("Translate: f")
        request.AllowWriteStreamBuffering = True

        Dim s As IO.Stream = request.GetRequestStream()
        Dim fs As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)

        'Number of KiloBytes in the file
        pb.Maximum = CInt(fileLength \ 1024)
        pb.Minimum = 0
        pb.Value = 0
        pb.Visible = True

        Dim bytes(1023) As Byte              '1024 bytes
        Dim bytesRead As Integer = 0
        Dim totalBytesRead As Long = 0

        '*** EVERYTHING HERE IS FAST ***
            bytesRead = fs.Read(bytes, 0, bytes.Length)

            If bytesRead > 0 Then

                totalBytesRead += bytesRead

                'Write to stream
                s.Write(bytes, 0, bytesRead)

                'Update progress
                pb.Value = CInt(totalBytesRead \ 1024)

            End If

        Loop While bytesRead > 0

        s = Nothing

        fs = Nothing

        '*** THIS IS REALLY SLOW ***
        Dim response As HttpWebResponse = _
            DirectCast(request.GetResponse(), HttpWebResponse)

        'Check the ContentLength of the Response
        Dim code As HttpStatusCode = response.StatusCode

        response = Nothing

        pb.Visible = False

        'Return Success
        Return (code = HttpStatusCode.OK)

    End Function
Question by:VBRocks
    LVL 20

    Expert Comment

    The slow part is where the actual transmission occurs. Are you sure there is no bandwidth consumption happeneing at that stage?
    Does using http instead of https speed things up?
    LVL 27

    Author Comment

    Hi thehagman!  Thanks for taking a look at my post.

    The site I'm accessing is a (WebDAV) secure transfer site (FTPES or HTTPS), so HTTP doesn't work, unfortunately.

    As far as the bandwidth consumption...  I'm not really sure where to begin with that.  ?

    Some of the research I've done has showed that computers are configured to download faster than upload, because the majority of data passed on the internet is being downloaded (such as webpages, images, files, etc.).

    The upload code actually seems comparable to My.Computer.Network.UploadFile().
    LVL 27

    Accepted Solution

    I found the answer to this question, finally.  I disabled AllowWriteStreamBuffering.  However, it wasn't entirely as easy as that... but the following Microsoft Support article provided the tips to make it work:


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now