PutFlieContent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* ==============================================================================
  2. * 功能描述:PutFlieContent
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/9/16 19:24:43
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace PackageUploader.Http
  14. {
  15. /// <summary>
  16. /// PutFlieContent
  17. /// </summary>
  18. class PutFlieContent : FileContent
  19. {
  20. private string m_Url;
  21. private Stream m_Stream;
  22. public PutFlieContent(string url, Stream stream)
  23. {
  24. m_Url = url;
  25. m_Stream = stream;
  26. }
  27. /// <summary>
  28. /// 上传文件
  29. /// </summary>
  30. /// <returns></returns>
  31. public virtual string PutContent()
  32. {
  33. var url = m_Url;
  34. var stream = m_Stream;
  35. if (stream == null)
  36. throw new ArgumentNullException(nameof(stream));
  37. var responseValue = string.Empty;
  38. HttpWebRequest request = null;
  39. try
  40. {
  41. var totalLength = stream.Length;
  42. request = (HttpWebRequest)WebRequest.Create(url);
  43. request.KeepAlive = true;
  44. request.Timeout = Timeout;
  45. request.Method = "POST";
  46. request.ContentLength = totalLength;
  47. request.ContentType = "multipart/form-data; ";
  48. #region 请求数据
  49. byte[] buffer = new byte[BufferLength];
  50. var transLength = 0;
  51. using (BinaryReader r = new BinaryReader(stream))
  52. {
  53. Stream postStream = request.GetRequestStream();
  54. try
  55. {
  56. int size = 0;
  57. do
  58. {
  59. size = r.Read(buffer, 0, buffer.Length);
  60. postStream.Write(buffer, 0, size);
  61. transLength += size;
  62. #region 调用事件
  63. StreamProgressArgs args = new StreamProgressArgs(size, transLength, totalLength);
  64. args.TransCompleted = (size == (long)0);
  65. TransferProgress?.Invoke(this, args);
  66. #endregion
  67. } while (size > 0);
  68. }
  69. finally
  70. {
  71. postStream?.Close();
  72. }
  73. }
  74. #endregion
  75. #region 相应数据
  76. //返回数据
  77. using (var response = (HttpWebResponse)request.GetResponse())
  78. {
  79. if (response.StatusCode != HttpStatusCode.OK)
  80. {
  81. var message = String.Format("请求失败. 获得http状态 {0}", response.StatusCode);
  82. throw new ApplicationException(message);
  83. }
  84. using (var responseStream = response.GetResponseStream())
  85. {
  86. if (responseStream != null)
  87. using (var reader = new StreamReader(responseStream))
  88. {
  89. responseValue = reader.ReadToEnd();
  90. }
  91. }
  92. }
  93. #endregion
  94. }
  95. catch (Exception e)
  96. {
  97. throw;
  98. }
  99. finally
  100. {
  101. //request?.dis
  102. }
  103. return responseValue;
  104. }
  105. }
  106. }