0
我正在尋找一個幫助,下面的下載文件:VB.NET的HttpWebRequest在塊
在我的Windows窗體應用程序,我下載我位於控制遠程服務器上的文件。我下面的代碼:
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方法發送塊。
如何避免在慢速連接上對較大的文件進行時間消耗?