C# - httpWebRequest 流的大小是否有限制?

C# - Is there a limit to the size of an httpWebRequest stream?

我正在尝试构建一个应用程序,使用 httpwebrequests 从自定义网络服务器下载一个小的二进制文件 (20-25 KB)。

这是服务器端代码:

Stream UpdateRequest = context.Request.InputStream;
byte[] UpdateContent = new byte[context.Request.ContentLength64];
UpdateRequest.Read(UpdateContent, 0, UpdateContent.Length);
String remoteVersion = "";
for (int i = 0;i < UpdateContent.Length;i++) { //check if update is necessary
    remoteVersion += (char)UpdateContent[i];
}

byte[] UpdateRequestResponse;

if (remoteVersion == remotePluginVersion) {
    UpdateRequestResponse = new byte[1];
    UpdateRequestResponse[0] = 0; //respond with a single byte set to 0 if no update is required
} else {
    FileInfo info = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), "remote logs", "PointAwarder.dll"));
    UpdateRequestResponse = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "remote logs", "PointAwarder.dll"));
    //respond with the updated file otherwise
}

//this byte is past the threshold and will not be the same in the version the client recieves
Console.WriteLine("5000th byte: " + UpdateRequestResponse[5000]);

//send the response
context.Response.ContentLength64 = UpdateRequestResponse.Length;
context.Response.OutputStream.Write(UpdateRequestResponse, 0, UpdateRequestResponse.Length);
context.Response.Close();

此后数组 UpdateRequestResponse 包含整个文件并已发送到客户端。

客户端运行这段代码:

//create the request
WebRequest request = WebRequest.Create(url + "pluginUpdate");
request.Method = "POST";
//create a byte array of the current version
byte[] requestContentTemp = version.ToByteArray();
int count = 0;
for (int i = 0; i < requestContentTemp.Length; i++) {
    if (requestContentTemp[i] != 0) {
        count++;
    }
}
byte[] requestContent = new byte[count];
for (int i = 0, j = 0; i < requestContentTemp.Length; i++) {
    if (requestContentTemp[i] != 0) {
        requestContent[j] = requestContentTemp[i];
        j++;
    }
}

//send the current version
request.ContentLength = requestContent.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(requestContent, 0, requestContent.Length);
dataStream.Close();

//get and read the response
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
byte[] responseBytes = new byte[response.ContentLength];
responseStream.Read(responseBytes, 0, (int)response.ContentLength);
responseStream.Close();
response.Close();

//if the response containd a single 0 we are up-to-date, otherwise write the content of the response to file
if (responseBytes[0] != 0 || response.ContentLength > 1) {
    BinaryWriter writer = new BinaryWriter(File.Open(Path.Combine(Directory.GetCurrentDirectory(), "ServerPlugins", "PointAwarder.dll"), FileMode.Create));
    writer.BaseStream.Write(responseBytes, 0, responseBytes.Length);
    writer.Close();
    TShockAPI.Commands.HandleCommand(TSPlayer.Server, "/reload");
}

客户端上的字节数组 responseBytes 应该与服务器上的数组 UpdateRequestResponse 相同,但事实并非如此。在大约 4000 个字节之后,每个字节都被设置为 0 而不是它应该是什么(responseBytes[3985] 是最后一个非零字节)。

发生这种情况是因为 httpWebRequest 有大小限制吗?我在我的代码中看不到任何可能导致它的错误,并且相同的代码在我只需要传递短数据序列(少于 100 字节)的其他情况下也可以工作。

MSDN 页面没有提到任何这样的大小限制。

这并不是说它有任何人为限制,这是 Streaming 您尝试做的事情的性质的副产品。我觉得下面一行是罪犯:

responseStream.Read(responseBytes, 0, (int)response.ContentLength);

我过去遇到过这个问题(使用 TCP 流),它没有读取数组的所有内容,因为它们还没有全部通过网络发送。这就是我要尝试的。

for (int i = 0; i < response.ContentLength; i++)
{
   responseBytes[i] = responseStream.ReadByte();
}

这样,它将确保一直读取到流的末尾。

编辑

usr 基于 BinaryReader 的解决方案效率更高。这是相关的解决方案:

BinaryReader binReader = new BinaryReader(responseStream);
const int bufferSize = 4096;
byte[] responseBytes;
using (MemoryStream ms = new MemoryStream())
{
    byte[] buffer = new byte[bufferSize];
    int count;
    while ((count = binReader.Read(buffer, 0, buffer.Length)) != 0)
        ms.Write(buffer, 0, count);
    responseBytes = ms.ToArray();
}

您假设 Read 正在读取您请求的字节数。但请求的计数只是一个上限。你必须容忍阅读小块。

您可以使用 var bytes = new BinaryReader(myStream).ReadBytes(count); 来读取准确的数字。不要太频繁地调用 ReadByte,因为那是非常 CPU 密集的。

最好的解决方案是放弃相当手动的 HttpWebRequest 并使用 HttpClientWebClient。所有这些都是自动为您完成的,您会得到 byte[].