12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /*-------------------------------------------------------------------------
- * 功能描述:HttpUtils
- * 作者:xulisong
- * 创建时间: 2019/7/29 10:16:56
- * 版本号:v1.0
- * -------------------------------------------------------------------------*/
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Http;
- namespace MBIRevitBase.Tools
- {
- /// <summary>
- /// HttpUtils信息处理
- /// </summary>
- public class HttpUtils
- {
- private static HttpClient m_Client;
- /// <summary>
- /// 创建HttpClient,单例对象生成
- /// </summary>
- /// <returns></returns>
- public static HttpClient CreateClient()
- {
- if (m_Client == null)
- {
- m_Client = new HttpClient();
- }
- return m_Client;
- }
- public const string WebKitFormBoundary = "----WebKitFormBoundary";
- public static BResult PostFormDataFile(string url,Stream stream)
- {
- using (HttpClient client = CreateClient())
- {
- string boundary = string.Format("{0}{1}", WebKitFormBoundary, DateTime.Now.Ticks.ToString("x"));
- MultipartFormDataContent content = new MultipartFormDataContent(boundary);
- #region Stream请求
- var streamContent = new StreamContent(stream);
- content.Add(streamContent, "file");
- #endregion
- var result = client.PostAsync(url, content).Result;
- try
- {
- if (result.IsSuccessStatusCode)
- {
- string rslt = result.Content.ReadAsStringAsync().Result;
- return new BResult(true,rslt);
- }
- }
- finally
- {
- client.Dispose();
- }
- }
- return string.Empty;
- }
- }
- }
|