VB.NET HttpWebRequest 分块下载文件

VB.NET HttpWebRequest download file in chunks

我正在寻求以下方面的帮助:

在我的 Windows Forms 应用程序中,我正在下载位于我控制的远程服务器上的文件。我的代码如下:

    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

问题是如果下载没有在指定时间内完成(带有超时设置的注释行),我们会收到超时异常。如果下载是分块的,我会看到驱动器上的文件越来越大。但是,不是这样,只有当响应完全完成时,文件才会出现。

在服务器端,我使用 response.outputstream.write 方法发送数据块。

如何才能在慢速连接上处理大文件时不超时?

我发现,只有当您发送 POST 请求时才会发生这种情况。我将参数更改为 URL 查询字符串的一部分,分块下载现在可以工作了。

    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