C#-httpWebRequest流的大小是否有限制? [英] C# - Is there a limit to the size of an httpWebRequest stream?

查看:183
本文介绍了C#-httpWebRequest流的大小是否有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build an application that downloads a small binary file (20-25 KB) from a custom webserver using httpwebrequests.

这是服务器端代码:

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包含整个文件,并已发送到客户端.

After this the array UpdateRequestResponse contains the entire file and has been sent to the client.

客户端运行以下代码:

//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,而不是应设置为0(responseBytes [3985]是最后一个非零字节).

The byte array responseBytes on the client should be identical to the array UpdateRequestResponse on the server, but it isn't. after about 4000 bytes every byte after that is set to 0 rather than what it should be (responseBytes[3985] is the last non-zero byte).

因为httpWebRequest有大小限制,会发生这种情况吗?我看不到代码中可能引起此错误的任何错误,并且在其他情况下,我只需要传递简短的数据序列(小于100个字节),就可以使用相同的代码.

Does this happen because httpWebRequest has a size limit? I can't see any bug in my code that could be causing it and the same code works in other instances where I only have to pass around short sequences of data (less than 100 bytes).

MSDN页面没有提及任何大小限制.

The MSDN pages don't mention any size limit like this.

推荐答案

这不是它有任何人为限制,这是您尝试执行的Streaming性质的副产品.我觉得以下行是罪犯:

It's not that it has any artificial limit, this is a byproduct of the Streaming nature of what you're attempting to do. I have a feeling the following line is the offender:

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

我过去曾遇到过这个问题(使用TCP流),它无法读取数组的所有内容,因为尚未通过有线发送它们.这就是我会尝试的.

I've had this issue in the past (with TCP streams), it doesn't read all of the contents of the array, because they haven't all been sent over the wire yet. This is what I would try instead.

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

这样,它将确保一直读取直到流结束.

That way, it will make sure to read all the way until the end of the stream.

编辑

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

usr's BinaryReader based solution is much more efficient. Here is the relevant solution:

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();
}

这篇关于C#-httpWebRequest流的大小是否有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆