HttpWebRequest - 通过块发送文件并跟踪进度 [英] HttpWebRequest - sending file by chunks and tracking progress

查看:205
本文介绍了HttpWebRequest - 通过块发送文件并跟踪进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,通过发送POST请求将文件上传到服务器:

  string fileName = @ C:\Console_sk.txt; 

HttpWebRequest request =(HttpWebRequest)HttpWebRequest.Create(http://+ Environment.MachineName +:8000 / Upload);
request.Method =POST;
request.ContentType =text / plain;
request.AllowWriteStreamBuffering = false;

stream fileStream = new FileStream(fileName,FileMode.Open);
request.ContentLength = fileStream.Length;

Stream serverStream = request.GetRequestStream();

byte [] buffer = new byte [4096];
while(true)
{
int bytesRead = fileStream.Read(buffer,0,buffer.Length);
if(bytesRead> 0)
{
serverStream.Write(buffer,0,bytesRead);
}
else
{
break;
}
}

serverStream.Close();
fileStream.Close();

request.GetResponse();

这将用于上传潜在的大文件,因此我们必须跟踪这个上传。



基于各种来源,我以为我的代码会上传4096个字节块的文件,即多个POST请求将被作出。但是,使用Fiddler跟踪请求时,它只显示一个POST请求。另一方面,当我启用 .NET网络跟踪时,生成的日志显示4096字节块确实被写入请求流,并且每个写入操作都始终打开套接字。



我对网络的了解总体来说很差,所以我不太明白这是如何工作的。当调用 serverStream.Write 时,提供的块是真的通过网络发送的,还是只是缓冲在某个地方?当> serverStream.Close 被调用时,我可以确定整个文件已经上传了吗? HttpWebRequest 类发送单个 > HTTP请求。你正在做的是你正在写入请求块,以避免加载整个文件在内存中。您正在从文件中读取数据,并直接以4KB块的形式写入请求流。
$ b


当调用serverStream.Write时,提供的块真的是通过网络发送的

是的,它被写入到网络套接字中。


I have the following code that uploads the file to the server by making the POST request:

string fileName = @"C:\Console_sk.txt";

HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create("http://" + Environment.MachineName + ":8000/Upload");
request.Method = "POST";
request.ContentType = "text/plain";
request.AllowWriteStreamBuffering = false;

Stream fileStream = new FileStream(fileName, FileMode.Open);
request.ContentLength = fileStream.Length;

Stream serverStream  = request.GetRequestStream();

byte[] buffer = new byte[4096];
while (true)
{
    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
    if (bytesRead > 0)
    {
        serverStream.Write(buffer, 0, bytesRead);     
    }
    else
    {
        break;
    }
}

serverStream.Close();
fileStream.Close();

request.GetResponse();

This will be used to upload potentially large files, therefore it is essential for us to track the progress of this upload.

Based on various sources, I've thought that my code will upload file in 4096 byte chunks i.e. multiple POST requests will be made. But when tracking requests by using Fiddler, it shows that only one POST request is made. On other hand, when I enabled .NET network tracing, the resulting log showed that 4096 byte chunks are indeed written to request stream and the socket is always being opened for each write operation.

My understanding of networking in general is quite poor, so I don't quite understand how this really works. When calling serverStream.Write, is the provided chunk really sent over the network, or is it just buffered somewhere? And when serverStream.Close is called, can I be sure that the whole file has been uploaded?

解决方案

The HttpWebRequest class sends a single HTTP request. What you are doing is that you are writing to the request in chunks to avoid loading the whole file in memory. You are reading from the file and directly writing to the request stream in chunks of 4KB.

When calling serverStream.Write, is the provided chunk really sent over the network

Yes, it is written to the network socket.

这篇关于HttpWebRequest - 通过块发送文件并跟踪进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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