0

我之前在下载 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
4

1 回答 1

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();
于 2013-07-27T23:26:41.603 回答