c# 模拟POST上传文件到服务器
2016-09-14 09:40
357 查看
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Net; namespace HuaTong.General.Utility { /// <summary> /// 用于模拟POST上传文件到服务器 /// </summary> public class HttpUpload { private ArrayList bytesArray; private Encoding encoding = Encoding.UTF8; private string boundary = String.Empty; public HttpUpload() { bytesArray = new ArrayList(); string flag = DateTime.Now.Ticks.ToString("x"); boundary = "---------------------------" + flag; } /// <summary> /// 合并请求数据 /// </summary> /// <returns></returns> private byte[] MergeContent() { int length = 0; int readLength = 0; string endBoundary = "--" + boundary + "--\r\n"; byte[] endBoundaryBytes = encoding.GetBytes(endBoundary); bytesArray.Add(endBoundaryBytes); foreach (byte[] b in bytesArray) { length += b.Length; } byte[] bytes = new byte[length]; foreach (byte[] b in bytesArray) { b.CopyTo(bytes, readLength); readLength += b.Length; } return bytes; } /// <summary> /// 上传 /// </summary> /// <param name="requestUrl">请求url</param> /// <param name="responseText">响应</param> /// <returns></returns> public bool Upload(String requestUrl, out String responseText) { WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary); byte[] responseBytes; byte[] bytes = MergeContent(); try { responseBytes = webClient.UploadData(requestUrl, bytes); responseText = System.Text.Encoding.UTF8.GetString(responseBytes); return true; } catch (WebException ex) { Stream responseStream = ex.Response.GetResponseStream(); responseBytes = new byte[ex.Response.ContentLength]; responseStream.Read(responseBytes, 0, responseBytes.Length); } responseText = System.Text.Encoding.UTF8.GetString(responseBytes); return false; } /// <summary> /// 设置表单数据字段 /// </summary> /// <param name="fieldName">字段名</param> /// <param name="fieldValue">字段值</param> /// <returns></returns> public void SetFieldValue(String fieldName, String fieldValue) { string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n"; string httpRowData = String.Format(httpRow, fieldName, fieldValue); bytesArray.Add(encoding.GetBytes(httpRowData)); } /// <summary> /// 设置表单文件数据 /// </summary> /// <param name="fieldName">字段名</param> /// <param name="filename">字段值</param> /// <param name="contentType">内容内型</param> /// <param name="fileBytes">文件字节流</param> /// <returns></returns> public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes) { string end = "\r\n"; string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string httpRowData = String.Format(httpRow, fieldName, filename, contentType); byte[] headerBytes = encoding.GetBytes(httpRowData); byte[] endBytes = encoding.GetBytes(end); byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length]; headerBytes.CopyTo(fileDataBytes, 0); fileBytes.CopyTo(fileDataBytes, headerBytes.Length); endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length); bytesArray.Add(fileDataBytes); } } }
相关文章推荐
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 通过WebClient模拟post上传文件到服务器
- C#模拟POST提交上传文件
- 通过WebClient模拟post上传文件到服务器
- C# Winform 上传本地文件到服务器路径
- IIS发布网站部分特殊配置--图片上传至别的服务器、iis配置 通配符应用程序映、iis配置 C#底层拦截.htm文件请求
- c# 监控服务器上传木马(包含可疑文件)
- [轉]ASP模拟POST提交请求上传文件
- C# Excel导入导出数据(二)——客户端上传文件到服务器
- rest client模拟带上传文件的post请求到ror中
- 分享C# 利用POST上传图片文件方法[完美]
- android模拟post请求上传文件
- ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
- C# Winform 上传本地文件到服务器路径
- 模拟php curl向远程服务器上传文件
- C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
- 通过 HTTP POST 上传文件到服务器.
- C#实现http协议支持上传下载文件的GET、POST请求
- c# 通过文件流方式上传文件到服务器