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

本文关键字:是否 有限制 httpWebRequest | 更新日期: 2023-09-27 17:56:31

我正在尝试构建一个应用程序,该应用程序使用 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 页面没有提到任何这样的大小限制。

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

这并不是说它有任何人为的限制,这是您尝试做的事情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[]