HttpUtils.cs 3.8 KB

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