HttpUtils.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. content.Add(streamContent, "file");
  41. #endregion
  42. var result = client.PostAsync(url, content).Result;
  43. try
  44. {
  45. if (result.IsSuccessStatusCode)
  46. {
  47. string rslt = result.Content.ReadAsStringAsync().Result;
  48. return new BResult(true,rslt);
  49. }
  50. }
  51. finally
  52. {
  53. client.Dispose();
  54. }
  55. }
  56. return string.Empty;
  57. }
  58. }
  59. }