123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- /*-------------------------------------------------------------------------
- * 功能描述:HttpUtils
- * 作者:xulisong
- * 创建时间: 2019/7/29 10:16:56
- * 版本号:v1.0
- * -------------------------------------------------------------------------*/
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Http;
- using MBIRevitBase.Result;
- using SAGA.DotNetUtils.Logger;
- 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);
- //"file.zip"必须有,它的格式可能影响到内部的一些配置
- content.Add(streamContent, "file", "file.zip");
- #endregion
- var result = client.PostAsync(url, content).Result;
- try
- {
- if (result.IsSuccessStatusCode)
- {
- string rslt = result.Content.ReadAsStringAsync().Result;
- return new BResult(true, rslt);
- }
- else
- {
- return result.ToString();
- }
- }
- finally
- {
- client.Dispose();
- }
- }
- return string.Empty;
- }
- /// <summary>
- /// 上传失败抛出异常,进行重试
- /// </summary>
- /// <param name="url"></param>
- /// <param name="stream"></param>
- /// <returns></returns>
- public static BResult PostFormDataFileThrowException(string url, Stream stream)
- {
- BResult rslt= (BResult)string.Empty;
- using (HttpClient client = CreateClient())
- {
- try
- {
- //超时时间设置为30分钟
- client.Timeout = new TimeSpan(0, 30, 0);
- string boundary = string.Format("{0}{1}", WebKitFormBoundary, DateTime.Now.Ticks.ToString("x"));
- MultipartFormDataContent content = new MultipartFormDataContent(boundary);
- #region Stream请求
- var streamContent = new StreamContent(stream);
- //"file.zip"必须有,它的格式可能影响到内部的一些配置
- content.Add(streamContent, "file", "file.zip");
- #endregion
- Log4Net.Debug("UploadStart");
- var tt = client.PostAsync(url, content);
- Log4Net.Debug("UploadEnd ");
- var result = tt.Result;
-
- Log4Net.Debug("UploadEnd "+ result.IsSuccessStatusCode);
- if (result.IsSuccessStatusCode)
- {
- rslt = (HttpResult) result.Content.ReadAsStringAsync().Result;
- }
- else
- {
- throw new Exception(result.ToString());
- }
- }
- catch (Exception e)
- {
- Log4Net.Debug("Upload Exception ");
- throw new Exception(e.ToString());
- }
- finally
- {
- Log4Net.Debug("Upload finally ");
- client.Dispose();
- }
- }
- return rslt;
- }
- }
- }
|