Post 方式 模仿 form表单 上传 图片 设置enctype = multipart/form-data
<form enctype="multipart/form-data"></form>
自定义 MultipartFormData 类 (4.5以上版本好像有这个类 类名称叫啥忘记了)
/// <summary> /// multipart/form-data 数据类型 /// </summary> public class MultipartFormData { public static string ContentType = "multipart/form-data; boundary=A300x"; private string headerStream = "--A300x\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; private string footerString = "--A300x--\r\n"; private string compart = "\r\n"; private string headerString = "--A300x\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n"; private List<byte> lists = new List<byte>(); private byte[] footerBytes; private byte[] comparts; public MultipartFormData() { footerBytes = Encoding.UTF8.GetBytes(footerString); comparts = Encoding.UTF8.GetBytes(compart); } public byte[] Buffer { get { return lists.ToArray(); } } private bool IsEnd = false; /// <summary> /// /// </summary> public void End() { if (!IsEnd) { IsEnd = true; //lists.AddRange(footerBytes.ToList()); this.AddRanges(footerBytes); } } public void AddRanges(byte[] array) { foreach (byte b in array) lists.Add(b); } /// <summary> /// 向序列化到多部/窗体数据 MIME 类型的 对象集合添加 HTTP 内容 /// </summary> /// <param name="content">要添加到集合中的 HTTP 内容</param> /// <param name="name">要添加的 HTTP 内容的名称</param> public void Add(string content, string name) { byte[] headerBytes = Encoding.UTF8.GetBytes(String.Format(headerString, name)); this.AddRanges(headerBytes); byte[] buffer = Encoding.UTF8.GetBytes(content); this.AddRanges(buffer); this.AddRanges(comparts); } /// <summary> /// 向序列化到多部/窗体数据 MIME 类型的 对象集合添加 HTTP 内容 /// </summary> /// <param name="content">要添加到集合中的 HTTP 内容</param> /// <param name="name">要添加的 HTTP 内容的名称</param> /// <param name="fileName">要添加到集合中的 HTTP 内容的文件名</param> public void Add(Stream content, string name, string fileName) { byte[] headerBytes = Encoding.UTF8.GetBytes(String.Format(headerStream, name, fileName)); this.AddRanges(headerBytes); byte[] buffer = new byte[content.Length]; content.Read(buffer, 0, buffer.Length); content.Seek(0, SeekOrigin.Begin); this.AddRanges(buffer); this.AddRanges(comparts); } /// <summary> /// 向序列化到多部/窗体数据 MIME 类型的 对象集合添加 HTTP 内容 /// </summary> /// <param name="content">要添加到集合中的 HTTP 内容</param> /// <param name="name">要添加的 HTTP 内容的名称</param> /// <param name="fileName">要添加到集合中的 HTTP 内容的文件名</param> public void Add(byte[] content, string name, string fileName) { byte[] headerBytes = Encoding.UTF8.GetBytes(String.Format(headerStream, name, fileName)); this.AddRanges(headerBytes); this.AddRanges(content); this.AddRanges(comparts); } }
上传图片代码
/// <summary> /// form-data 数据类型传递数据 /// </summary> private MultipartFormData multiparFormData = new MultipartFormData(); /// <summary> /// 键值对应 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void AddFormData(string key, string value) { this.multiparFormData.Add(value, key); } /// <summary> /// 传递一个 strem 类型 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="fileName"></param> public void AddFormData(string key, Stream value, string fileName) { this.multiparFormData.Add(value, key, fileName); } /// <summary> /// 传递一个 byte[] 类型 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="fileName"></param> public void AddFormData(string key, byte[] value, string fileName) { this.multiparFormData.Add(value, key, fileName); } private static CookieContainer m_Cookie = new CookieContainer(); /// <summary> /// POST form-data 方式请求消息 /// </summary> /// <param name="url"></param> /// <param name="isHttps"></param> /// <returns></returns> public string PostFromData(string url, bool isHttps) { try { multiparFormData.End(); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; request.ContentType = MultipartFormData.ContentType; request.CookieContainer = m_Cookie;//设置上一个访问页面的cookie 保持session request.ContentLength = multiparFormData.Buffer.Length; Stream stream = request.GetRequestStream(); stream.Write(multiparFormData.Buffer, 0, multiparFormData.Buffer.Length); stream.Close(); WebResponse response = request.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader sr = new StreamReader(streamResponse); return sr.ReadToEnd(); } catch (Exception ex) { Log.WriteLog("POST form-data 方式请求消息:" + ex.Message); return "error"; } }
时间: 2024-11-05 07:58:02