VB.NET HttpWebRequest下载文件(成块) [英] VB.NET HttpWebRequest download file in chunks
问题描述
我正在寻找有关以下方面的帮助:
I'm looking for a help with following:
在Windows窗体应用程序中,我正在下载位于我控制的远程服务器上的文件.我的代码如下:
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.
在服务器端,我正在使用response.outputstream.write方法发送数据块.
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?
推荐答案
我发现,仅当您发送POST请求时才会发生这种情况.我将参数更改为URL查询字符串的一部分,并且分块下载现在可以正常工作.
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
这篇关于VB.NET HttpWebRequest下载文件(成块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!