Httpweb请求将zip文件下载到指定的路径,但内容未下载。档案只有1kb

时间:2019-05-31 10:05:59

标签: vb.net download stream zip httpwebrequest

我正在使用VB.NET转到URL,并使用HttpWebRequest访问和下载内容(一个zip文件)。

如果我手动转到页面,则ZIP会以文件名ABC_1902.zip下载,并且工作正常。当我尝试使用我的代码时,它无法正常工作。该文件只有1KB。一个区别是我试图用不同的名称创建一个新文件,并将zip的内容添加到正在创建的这个新文件中。

它创建文件,但只有1kb,一个空的zip文件夹。下载的内容未添加。当我手动转到URL时,它可以工作,文件下载到我的Downloads文件夹中,大小为13,813KB。

Dim strEBD_URL As String = URL + "/ABC_1902.zip"

Dim webRequest As HttpWebRequest = Net.WebRequest.Create(strEBD_URL)
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.Method = "post"
webRequest.Headers.Add(HttpRequestHeader.Cookie, WebBrowser1.Document.Cookie)

applog("Starting to download ZIP file", 1)
Me.Refresh()

Dim bytesProcessed As Integer = 0
Dim remoteStream As Stream
Dim localStream As Stream
Dim response As WebResponse

response = webRequest.GetResponse()
If Not response Is Nothing Then
    remoteStream = response.GetResponseStream()
    localStream = File.Create(TextBox8.
    Dim myBuffer As Byte()
    ReDim myBuffer(1024)
    Dim bytesRead As Integer
    bytesRead = remoteStream.Read(myBuffer, 0, 1024)

    Do While (bytesRead > 0)
        localStream.Write(myBuffer, 0, bytesRead)
        bytesProcessed += bytesRead
        bytesRead = remoteStream.Read(myBuffer, 0, 1024)
    Loop
    localStream.Close()

End If

TextBox8.Text是用于创建文件的文件路径

0 个答案:

没有答案