HttpUpload.cs 4.2 KB

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