当前位置:   article > 正文

后台使用multipart/form-data方式提交数据_后端提交multipart/form-data

后端提交multipart/form-data
  1. protected string CreateFormDateResponse(string url, Encoding encoding, IDictionary<string, string> textParams, IDictionary<string, FileinFo> fileParams)
  2. {
  3. try
  4. {
  5. HttpWebRequest request;
  6. // 重新设置请求头
  7. request = (HttpWebRequest)WebRequest.Create(url);
  8. string host = url.Replace("http://", "").Replace("https://", "");
  9. host = host.Remove(host.IndexOf('/'));
  10. request.Host = host;
  11. request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8;";
  12. request.KeepAlive = true;
  13. request.Referer = url;
  14. request.ProtocolVersion = HttpVersion.Version11;
  15. request.Method = "POST";
  16. string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
  17. request.ContentType = "multipart/form-data;charset=gb2312;boundary=" + boundary;
  18. request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; LCTE; rv:11.0) like Gecko";
  19. request.Timeout = 5000;
  20. request.Headers["Upgrade-Insecure-Requests"] = "1";
  21. request.Headers["Accept-Encoding"] = "gzip, deflate";
  22. request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";
  23. request.Headers["Cache-Control"] = "no-cache";
  24. request.Headers["Pragma"] = "no-cache";
  25. // 自动追踪30X跳转
  26. request.AllowAutoRedirect = true;
  27. byte[] itemBoundaryBytes = encoding.GetBytes("\r\n--" + boundary + "\r\n");
  28. byte[] endBoundaryBytes = encoding.GetBytes("\r\n--" + boundary + "--\r\n");
  29. // 组装文本请求参数
  30. string textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}";
  31. IEnumerator<KeyValuePair<string, string>> textEnum = textParams.GetEnumerator();
  32. using (System.IO.Stream reqStream = request.GetRequestStream())
  33. {
  34. while (textEnum.MoveNext())
  35. {
  36. string textEntry = string.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value);
  37. byte[] itemBytes = encoding.GetBytes(textEntry);
  38. reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
  39. reqStream.Write(itemBytes, 0, itemBytes.Length);
  40. }
  41. // 组装文件请求参数
  42. string fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
  43. IEnumerator<KeyValuePair<string, FileinFo>> fileEnum = fileParams.GetEnumerator();
  44. while (fileEnum.MoveNext())
  45. {
  46. string key = fileEnum.Current.Key;
  47. FileinFo fileItem = fileEnum.Current.Value;
  48. string fileEntry = string.Format(fileTemplate, key, fileItem.filename, fileItem.ContentType);
  49. byte[] itemBytes = encoding.GetBytes(fileEntry);
  50. reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
  51. reqStream.Write(itemBytes, 0, itemBytes.Length);
  52. byte[] fileBytes = fileItem.data;
  53. reqStream.Write(fileBytes, 0, fileBytes.Length);
  54. }
  55. reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  56. reqStream.Close();
  57. }
  58. ServicePointManager.DefaultConnectionLimit = 200;
  59. using (HttpWebResponse rsp = (HttpWebResponse)request.GetResponse())
  60. {
  61. try
  62. { // 以字符流的方式读取HTTP响应
  63. using (Stream stream = rsp.GetResponseStream())
  64. {
  65. using (StreamReader reader = new StreamReader(stream, encoding))
  66. {
  67. return reader.ReadToEnd();
  68. }
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. LogInfo.Error("响应文本错误", ex);
  74. return null;
  75. }
  76. finally
  77. {
  78. // 释放资源
  79. if (rsp != null) rsp.Close();
  80. }
  81. }
  82. }
  83. catch (WebException e)
  84. {
  85. LogInfo.Error("WebException异常url=" + url + ";\r\ndatabuffer=", e);
  86. return string.Empty;
  87. }
  88. catch (Exception e)
  89. {
  90. LogInfo.Error("异常url=" + url, e);
  91. return string.Empty;
  92. }
  93. }
  94. public class FileinFo
  95. {
  96. public string name { set; get; }
  97. public string filename { set; get; }
  98. public string ContentType { set; get; }
  99. public byte[] data { set; get; }
  100. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/302949
推荐阅读
相关标签
  

闽ICP备14008679号