VB.NET HttpWebRequest download file in chunks

主宰稳场 提交于 2019-12-10 12:17:27

问题


I'm looking for a help with following:

In my Windows Forms application, I'm downloading the file located on remote server I control. My code below:

    Public Shared Function DownloadFileWithPOST(ByVal httpPath As String, ByVal storePath As String, postdata As String) As String
    Try
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Dim postdatabytes As Byte()

        theRequest = HttpWebRequest.Create(httpPath)


        If My.Settings.ProxyURL <> "" Then
            Dim prx As New WebProxy(My.Settings.ProxyURL)
            theRequest.Proxy = prx
        End If


        '  theRequest.Timeout = My.Settings.RequestTimeout

        postdatabytes = System.Text.Encoding.UTF8.GetBytes(postdata)
        theRequest.Method = "POST"
        theRequest.ContentType = "application/x-www-form-urlencoded"
        theRequest.ContentLength = postdatabytes.Length

        Using stream = theRequest.GetRequestStream()
            stream.Write(postdatabytes, 0, postdatabytes.Length)
        End Using



        theResponse = theRequest.GetResponse


        Dim length As Double = theResponse.ContentLength




        Dim writeStream As New IO.FileStream(storePath, IO.FileMode.Create)


        Dim nRead As Integer

        Dim readBytes(4095) As Byte
        Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)



        Do Until bytesread = 0



            'speedtimer.Start()


            nRead += bytesread



            writeStream.Write(readBytes, 0, bytesread)


            bytesread = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
        Loop

        'Close the streams
        theResponse.GetResponseStream.Close()
        writeStream.Close()


        Return "OK"
    Catch ex As Exception
        Return ex.Message
    End Try
End Function

The problem is that we're getting timeout exception if the download is not completed in specified time (the commented line with timeout setting). If the download is chunked, I would see the file on drive growing on it size. However, instead of this, only when response is completely finished the file appears.

On the server side, I'm sending chunks by using response.outputstream.write method.

What to do to not get time-outed on larger files on slow connection?


回答1:


I found, that this occurs only when you send POST request. I changed the params to be part as URL querystring and the chunked downloading now works.

    Public Shared Function DownloadFileWithQueryString(ByVal httpPath As String, ByVal storePath As String, querystring As String) As String
    Try
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Dim fullurl As String = httpPath & "?" & querystring
        theRequest = HttpWebRequest.Create(fullurl)


        If My.Settings.ProxyURL <> "" Then
            Dim prx As New WebProxy(My.Settings.ProxyURL)
            theRequest.Proxy = prx
        End If




        theResponse = theRequest.GetResponse

        Dim length As Double = theResponse.ContentLength




        Dim writeStream As New IO.FileStream(storePath, IO.FileMode.Create)


        Dim nRead As Integer

        Dim readBytes(4095) As Byte
        Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)



        Do Until bytesread = 0



            'speedtimer.Start()


            nRead += bytesread



            writeStream.Write(readBytes, 0, bytesread)


            bytesread = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
        Loop

        'Close the streams
        theResponse.GetResponseStream.Close()
        writeStream.Close()


        Return "OK"
    Catch ex As Exception
        Return ex.Message
    End Try
End Function


来源:https://stackoverflow.com/questions/38558598/vb-net-httpwebrequest-download-file-in-chunks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!