HttpUtils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:HttpUtils
  3. * 作者:xulisong
  4. * 创建时间: 2019/7/29 10:16:56
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Net.Http;
  11. using MBIRevitBase.Result;
  12. using SAGA.DotNetUtils.Logger;
  13. namespace MBIRevitBase.Tools
  14. {
  15. /// <summary>
  16. /// HttpUtils信息处理
  17. /// </summary>
  18. public class HttpUtils
  19. {
  20. private static HttpClient m_Client;
  21. /// <summary>
  22. /// 创建HttpClient,单例对象生成
  23. /// </summary>
  24. /// <returns></returns>
  25. public static HttpClient CreateClient()
  26. {
  27. //if (m_Client == null)
  28. {
  29. m_Client = new HttpClient();
  30. }
  31. return m_Client;
  32. }
  33. public const string WebKitFormBoundary = "----WebKitFormBoundary";
  34. public static BResult PostFormDataFile(string url, Stream stream)
  35. {
  36. using (HttpClient client = CreateClient())
  37. {
  38. string boundary = string.Format("{0}{1}", WebKitFormBoundary, DateTime.Now.Ticks.ToString("x"));
  39. MultipartFormDataContent content = new MultipartFormDataContent(boundary);
  40. #region Stream请求
  41. var streamContent = new StreamContent(stream);
  42. //"file.zip"必须有,它的格式可能影响到内部的一些配置
  43. content.Add(streamContent, "file", "file.zip");
  44. #endregion
  45. var result = client.PostAsync(url, content).Result;
  46. try
  47. {
  48. if (result.IsSuccessStatusCode)
  49. {
  50. string rslt = result.Content.ReadAsStringAsync().Result;
  51. return new BResult(true, rslt);
  52. }
  53. else
  54. {
  55. return result.ToString();
  56. }
  57. }
  58. finally
  59. {
  60. client.Dispose();
  61. }
  62. }
  63. return string.Empty;
  64. }
  65. /// <summary>
  66. /// 上传失败抛出异常,进行重试
  67. /// </summary>
  68. /// <param name="url"></param>
  69. /// <param name="stream"></param>
  70. /// <returns></returns>
  71. public static BResult PostFormDataFileThrowException(string url, Stream stream)
  72. {
  73. BResult rslt= (BResult)string.Empty;
  74. using (HttpClient client = CreateClient())
  75. {
  76. try
  77. {
  78. //超时时间设置为30分钟
  79. client.Timeout = new TimeSpan(0, 30, 0);
  80. string boundary = string.Format("{0}{1}", WebKitFormBoundary, DateTime.Now.Ticks.ToString("x"));
  81. MultipartFormDataContent content = new MultipartFormDataContent(boundary);
  82. #region Stream请求
  83. var streamContent = new StreamContent(stream);
  84. //"file.zip"必须有,它的格式可能影响到内部的一些配置
  85. content.Add(streamContent, "file", "file.zip");
  86. #endregion
  87. Log4Net.Debug("UploadStart");
  88. var tt = client.PostAsync(url, content);
  89. Log4Net.Debug("UploadEnd ");
  90. var result = tt.Result;
  91. Log4Net.Debug("UploadEnd "+ result.IsSuccessStatusCode);
  92. if (result.IsSuccessStatusCode)
  93. {
  94. rslt = (HttpResult) result.Content.ReadAsStringAsync().Result;
  95. }
  96. else
  97. {
  98. throw new Exception(result.ToString());
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. Log4Net.Debug("Upload Exception ");
  104. throw new Exception(e.ToString());
  105. }
  106. finally
  107. {
  108. Log4Net.Debug("Upload finally ");
  109. client.Dispose();
  110. }
  111. }
  112. return rslt;
  113. }
  114. }
  115. }