winform HttpWebRequest上传文件
Posted 码农-小菜鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform HttpWebRequest上传文件相关的知识,希望对你有一定的参考价值。
Winform点击button按钮上传:
string filePath = "E:\\test.rar"; string fileName = "test.rar"; string postURL = "http://localhost:5995/Default.aspx"; // 边界符 var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); // 最后的结束符 var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n"); // 文件参数头 const string filePartHeader = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n"; var fileHeader = string.Format(filePartHeader, "file", fileName); var fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader); // 开始拼数据 var memStream = new MemoryStream(); memStream.Write(beginBoundary, 0, beginBoundary.Length); // 文件数据 memStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length); var buffer = new byte[1024]; int bytesRead; // =0 while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } fileStream.Close(); // Key-Value数据 var stringKeyHeader = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"" + "\r\n\r\n{1}\r\n"; Dictionary<string, string> stringDict = new Dictionary<string, string>(); stringDict.Add("len", "500"); stringDict.Add("wid", "300"); foreach (byte[] formitembytes in from string key in stringDict.Keys select string.Format(stringKeyHeader, key, stringDict[key]) into formitem select Encoding.UTF8.GetBytes(formitem)) { memStream.Write(formitembytes, 0, formitembytes.Length); } // 写入最后的结束边界符 memStream.Write(endBoundary, 0, endBoundary.Length); //倒腾到tempBuffer? memStream.Position = 0; var tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); // 创建webRequest并设置属性 var webRequest = (HttpWebRequest)WebRequest.Create(postURL); webRequest.Method = "POST"; webRequest.Timeout = 100000; webRequest.ContentType = "multipart/form-data; boundary=" + boundary; webRequest.ContentLength = tempBuffer.Length; var requestStream = webRequest.GetRequestStream(); requestStream.Write(tempBuffer, 0, tempBuffer.Length); requestStream.Close(); var httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); string responseContent; using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"))) { responseContent = httpStreamReader.ReadToEnd(); } httpWebResponse.Close(); webRequest.Abort();
Web站点Default.aspx页面:
Response.ContentType = "text/plain"; if (Request.Files.Count == 0) { Response.Write("No file"); return; } HttpPostedFile f1 = Request.Files[0]; f1.SaveAs("E:\\test-upload.rar");
string strPars = "";
foreach (var key in Request.Form.AllKeys)
{
string val = Request[key];
strPars += "[" + key + ":" + val + "] ";
}
image.Dispose();
Response.Write("OK Get File:" + f1.FileName + " Pars:" + strPars);
以上是关于winform HttpWebRequest上传文件的主要内容,如果未能解决你的问题,请参考以下文章
急急急!C#中winform发送 HttpWebRequest请求,不能连续发送
c#中我要实现大量的httpwebrequest,但是不想winform假死住,请问用多线程还是异步?
c#winform使用 httpwebrequest请求和httpwebresponse响应,速度有时很慢,不知道是啥原因!