문제를 통해 부분으로 데이터를 업로드 :어떻게 HttpWebRequest를
나는 하나의 HTTP 요청을 통해 덩어리로 데이터를 업로드하고 (인터넷을 통해 데이터를 전송 phisical) 각 업로드 후 진행의 변화를 보여주고 싶다. (이제 업로드 진행 상황을 보여주는 방법이 중요하지 않습니다. 단순히 콘솔에 데이터를 출력 할 수 있습니다).
코드 :
Stackoverlow 많은 같은 질문이 : link 1 등 (나는 충분한 명성이 없기 때문에 내가 더 많은 링크를 포함 할 수 없습니다).
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
}
비고 :
this link에 따라 또한, 각 각 요청 스트림에 기록하는 동안 발생해야 내보내지만, 실제로는 (은 피들러에서 설명 될 수있다) 모든 전송 작업에만 발생 요청 스트림을 닫은 후 또는 응답 만 얻고 이전에는 그렇지 않습니다. (모두 SendChuncked
, AllowWriteStreamBuffering
및 ContentLength
매개 변수에 따라 다르지만 스트림에 쓸 때마다 데이터가 보내지지 않습니다.
질문 : 각 후
데이터 (물리적으로) 보낼 수있는 방법이 (Write 메서드의 각 호출)를 작성?
제약 :
순 2.0;
HttpWebRequest 프리미티브 (WebClient 아님) 만 사용. 아무도이 질문에 대답,하지만이 질문은 zergatul 사용자가 러시아어 유래에 대한 답이