我之前在下载rar文件时遇到问题,服务器没有为rar配置,所以我决定给一个go zip,这里又是另一个问题。我添加了一个异常并得到如下消息:“指定的参数超出了有效值的范围。参数名称:大小” 文件大约100KB。 期待你的帮助,谢谢。
这是我的代码:
request = Net.HttpWebRequest.Create("http://webserver.com/test/zip")
request.CookieContainer = logincookie
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 Java/1.7.0"
request.Method = "GET"
response = request.GetResponse()
If Not response.ContentType = "application/zip" Then
MsgBox("Error: " & ErrorToString() & vbCrLf & "Current file name is : " & response.ContentType.ToString)
Exit Sub
End If
Dim intLen As Int32 = response.ContentLength
Dim memStream As MemoryStream
Using stmResponse As IO.Stream = response.GetResponseStream()
Dim buffer = New Byte(intLen) {}
Dim bytesRead As Integer
Do
bytesRead += stmResponse.Read(buffer, bytesRead, intLen - bytesRead)
Loop Until bytesRead = intLen
memStream = New MemoryStream(buffer)
My.Computer.FileSystem.WriteAllBytes("test.zip", buffer, False)
End Using
End If
答案 0 :(得分:0)
为什么ContentLength
为-1?可能是服务器以块(Transfer-Encoding: chunked
)
由于您的原始问题包含c#
标记,我将使用c#回答它,而不使用 ContentLength
var request = HttpWebRequest.Create(""http://webserver.com/test/zip") as HttpWebRequest;
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0 Java/1.7.0";
request.Method = "GET";
var response = request.GetResponse();
var stmResponse = response.GetResponseStream();
MemoryStream memStream = new MemoryStream();
stmResponse.CopyTo(memStream);
byte[] buffer = memStream.ToArray();