服务端 mvc webapi写法:
Dictionary<string, string> dic = new Dictionary<string, string>(); //multipart/form-data需要用以下方式接收数据 if (Request.Content.IsMimeMultipartContent()) { var multipartMemoryStreamProvider = await Request.Content.ReadAsMultipartAsync(); foreach (var content in multipartMemoryStreamProvider.Contents) { if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName)) { using (Stream stream = await content.ReadAsStreamAsync()) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); } } else { string val = await content.ReadAsStringAsync(); dic.Add(content.Headers.ContentDisposition.Name.Replace(""", ""), System.Web.HttpUtility.UrlDecode(val)); } } JsonConvert.PopulateObject(JsonConvert.SerializeObject(dic), input); }
客户端调用 模拟 multipart/form-data 形式组装成字符串:
public string postFileMessage(string strUrl, List<PostDateClass> postParaList) { try { string responseContent = ""; var memStream = new MemoryStream(); var webRequest = (HttpWebRequest)WebRequest.Create(strUrl); // 边界符 var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); // 边界符 var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + " "); // 最后的结束符 var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "-- "); memStream.Write(beginBoundary, 0, beginBoundary.Length); // 设置属性 webRequest.Method = "POST"; webRequest.Timeout = 10000; webRequest.ContentType = "multipart/form-data; boundary=" + boundary; for (int i = 0; i < postParaList.Count; i++) { PostDateClass temp = postParaList[i]; if (temp.Type == 1) { var fileStream = new FileStream(temp.Value, FileMode.Open, FileAccess.Read); // 写入文件 const string filePartHeader = "Content-Disposition: form-data; name="{0}"; filename="{1}" " + "Content-Type: application/octet-stream "; var header = string.Format(filePartHeader, temp.Prop, temp.Value); var headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); var buffer = new byte[1024]; int bytesRead; // =0 while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); } string end = " "; headerbytes = Encoding.UTF8.GetBytes(end); memStream.Write(headerbytes, 0, headerbytes.Length); fileStream.Close(); } else if (temp.Type == 0) { // 写入字符串的Key var stringKeyHeader = "Content-Disposition: form-data; name="{0}"" + " {1} "; var header = string.Format(stringKeyHeader, temp.Prop, temp.Value); var headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); } if (i != postParaList.Count - 1) memStream.Write(beginBoundary, 0, beginBoundary.Length); else // 写入最后的结束边界符 memStream.Write(endBoundary, 0, endBoundary.Length); } webRequest.ContentLength = memStream.Length; var requestStream = webRequest.GetRequestStream(); memStream.Position = 0; var tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); requestStream.Write(tempBuffer, 0, tempBuffer.Length); requestStream.Close(); using (HttpWebResponse res = (HttpWebResponse)webRequest.GetResponse()) { using (Stream resStream = res.GetResponseStream()) { byte[] buffer = new byte[1024]; int read; while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0) { responseContent += Encoding.UTF8.GetString(buffer, 0, read); } } res.Close(); } return responseContent; } catch (Exception e) { } return null; }