Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过HttpWebRequest按部分上传数据_C#_.net_System.net.httpwebrequest - Fatal编程技术网

C# 如何通过HttpWebRequest按部分上传数据

C# 如何通过HttpWebRequest按部分上传数据,c#,.net,system.net.httpwebrequest,C#,.net,System.net.httpwebrequest,问题: 我想通过一个http请求分块上传数据,并在每次上传后显示进度变化(通过互联网发送数据)。(现在,如何显示上传进度并不重要。我可以简单地将一些数据输出到控制台) 代码: Stackoverlow有很多这样的问题: 等(我不能包括更多的链接,因为我没有足够的声誉) 使用系统; 使用系统文本; 使用System.IO; Net系统; ... 公共静态无效上载数据() { const string data=“简单字符串”; byte[]buffer=new ascienceoding().Ge

问题

我想通过一个http请求分块上传数据,并在每次上传后显示进度变化(通过互联网发送数据)。(现在,如何显示上传进度并不重要。我可以简单地将一些数据输出到控制台)

代码

Stackoverlow有很多这样的问题: 等(我不能包括更多的链接,因为我没有足够的声誉)

使用系统;
使用系统文本;
使用System.IO;
Net系统;
...
公共静态无效上载数据()
{
const string data=“简单字符串”;
byte[]buffer=new ascienceoding().GetBytes(数据);
//多亏了http://www.posttestserver.com 一切都在盒子里工作
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(“http://posttestserver.com/post.php");
req.UserAgent=“Mozilla/5.0(Windows;U;Windows NT 6.1;en-US)AppleWebKit/534.10”+
“(KHTML,像壁虎一样)Chrome/8.0.552.224 Safari/534.10”;
req.Accept=“application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5”;
请求标题添加(“接受字符集”、“ISO-8859-1,utf-8;q=0.7,*;q=0.3”);
请求标题添加(“接受语言”,“en-US,en;q=0.8”);
请求方法=“POST”;
req.ContentType=“应用程序/x-www-form-urlencoded”;
req.ContentLength=缓冲区长度;
req.SendChunked=true;
int bytesRead=buffer.Length;
常量int chunkSize=3;
流s=req.GetRequestStream();
对于(int offset=0;offsetchunkSize?chunkSize:bytesLeft;
s、 写入(缓冲区、偏移量、字节写入);
}
s、 Close();//重要提示:仅在此处发送所有数据
}
备注

也根据 , 每次发送都必须在每次写入请求流的过程中发生,但实际上(可以在Fiddler中演示)所有发送操作仅在请求流关闭后发生,或者仅通过获取响应而不是更早发生。(全部取决于
SendChuncked
AllowWriteStreamBuffering
ContentLength
参数,但每次写入流后都不会发送数据

问题

每次写入(每次调用Write方法)后如何(物理)发送数据

限制条件:

  • 净额2.0

  • 仅使用HttpWebRequest原语(非WebClient)

    • 因为没有人回答这个问题,但是这个问题已经被用户在俄语stackoverflow上回答了,所以我将在这里发布


      答案
      这是你所期望的。我用的是微软网络监视器。这是一个很好的实用程序,并且是免费的(与httpdebugger相反)。我在NET2.0中调试了你的代码

      网络监视器显示每个3字节的发送(我使用了更长的字符串)

      此处已发送文本“ple”(“simplestring”)


      备注
      在第一张图片中,字符串
      //САЖззззаааачззза

      表示
      //重要提示:只有这里的数据将通过网络发送

      因为没有人回答这个问题,但是这个问题已经由用户在俄语stackoverflow上回答了,我将在这里发布


      答案
      这是你所期望的。我用的是微软网络监视器。这是一个很好的实用程序,并且是免费的(与httpdebugger相反)。我在NET2.0中调试了你的代码

      网络监视器显示每个3字节的发送(我使用了更长的字符串)

      此处已发送文本“ple”(“simplestring”)


      备注
      在第一张图片中,字符串
      //САЖззззаааачззза

      表示
      //重要提示:只有此处的数据将通过网络发送

      using System;
      using System.Text;
      using System.IO;
      using System.Net;
      
      ...
      
      public static void UploadData()
      {
          const string data = "simple string";
          byte[] buffer = new ASCIIEncoding().GetBytes(data);
      
          // Thanks to http://www.posttestserver.com all is working from the box
          HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://posttestserver.com/post.php");
          req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 " + 
                          "(KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
          req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
          req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
          req.Headers.Add("Accept-Language", "en-US,en;q=0.8");
          req.Method = "POST";
          req.ContentType = "application/x-www-form-urlencoded";
          req.ContentLength = buffer.Length;            
          req.SendChunked = true;            
      
          int bytesRead = buffer.Length;
          const int chunkSize = 3;
          Stream s = req.GetRequestStream();
          for (int offset = 0; offset < bytesRead; offset += chunkSize)
          {
              int bytesLeft = bytesRead - offset;
              int bytesWrite = bytesLeft > chunkSize ? chunkSize : bytesLeft;
              s.Write(buffer, offset, bytesWrite);
          }
          s.Close(); // IMPORTANT: only here all data will be send
      }