HttpUpload.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. namespace MySystem
  8. {
  9. public class HttpUpload
  10. {
  11. private ArrayList bytesArray;
  12. private Encoding encoding = Encoding.UTF8;
  13. private string boundary = String.Empty;
  14. public HttpUpload()
  15. {
  16. bytesArray = new ArrayList();
  17. // string flag = DateTime.Now.Ticks.ToString("x");
  18. // boundary = "---------------------------" + flag;
  19. }
  20. /// <summary>
  21. /// 合并请求数据
  22. /// </summary>
  23. /// <returns></returns>
  24. private byte[] MergeContent()
  25. {
  26. int length = 0;
  27. int readLength = 0;
  28. // string endBoundary = "--" + boundary + "--\r\n";
  29. // byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
  30. // bytesArray.Add(endBoundaryBytes);
  31. foreach (byte[] b in bytesArray)
  32. {
  33. length += b.Length;
  34. }
  35. byte[] bytes = new byte[length];
  36. foreach (byte[] b in bytesArray)
  37. {
  38. b.CopyTo(bytes, readLength);
  39. readLength += b.Length;
  40. }
  41. return bytes;
  42. }
  43. /// <summary>
  44. /// 上传
  45. /// </summary>
  46. /// <param name="requestUrl">请求url</param>
  47. /// <param name="responseText">响应</param>
  48. /// <returns></returns>
  49. public bool Upload(String requestUrl, out String responseText, Dictionary<string, string> Headers)
  50. {
  51. WebClient webClient = new WebClient();
  52. foreach (string key in Headers.Keys)
  53. {
  54. webClient.Headers.Add(key, Headers[key]);
  55. }
  56. webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=boundary");
  57. byte[] responseBytes;
  58. byte[] bytes = MergeContent();
  59. try
  60. {
  61. responseBytes = webClient.UploadData(requestUrl, bytes);
  62. responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
  63. return true;
  64. }
  65. catch (WebException ex)
  66. {
  67. Stream responseStream = ex.Response.GetResponseStream();
  68. responseBytes = new byte[ex.Response.ContentLength];
  69. responseStream.Read(responseBytes, 0, responseBytes.Length);
  70. }
  71. responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
  72. return false;
  73. }
  74. /// <summary>
  75. /// 设置表单数据字段
  76. /// </summary>
  77. /// <param name="fieldName">字段名</param>
  78. /// <param name="fieldValue">字段值</param>
  79. /// <returns></returns>
  80. public void SetFieldValue(String fieldName, String fieldValue)
  81. {
  82. string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
  83. string httpRowData = String.Format(httpRow, fieldName, fieldValue);
  84. bytesArray.Add(encoding.GetBytes(httpRowData));
  85. }
  86. public void SetFieldValueForCus(String content)
  87. {
  88. bytesArray.Add(encoding.GetBytes(content));
  89. }
  90. /// <summary>
  91. /// 设置表单文件数据
  92. /// </summary>
  93. /// <param name="fieldName">字段名</param>
  94. /// <param name="filename">字段值</param>
  95. /// <param name="contentType">内容内型</param>
  96. /// <param name="fileBytes">文件字节流</param>
  97. /// <returns></returns>
  98. public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes)
  99. {
  100. string end = "\r\n";
  101. string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
  102. string httpRowData = String.Format(httpRow, fieldName, filename, contentType);
  103. byte[] headerBytes = encoding.GetBytes(httpRowData);
  104. byte[] endBytes = encoding.GetBytes(end);
  105. byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];
  106. headerBytes.CopyTo(fileDataBytes, 0);
  107. fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
  108. endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);
  109. bytesArray.Add(fileDataBytes);
  110. }
  111. }
  112. }