HttpUtils.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. }
  64. }