HttpUtils.cs 3.7 KB

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