123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
-
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Http;
- using MBIRevitBase.Result;
- namespace MBIRevitBase.Tools
- {
-
-
-
- public class HttpUtils
- {
- private static HttpClient m_Client;
-
-
-
-
- public static HttpClient CreateClient()
- {
-
- {
- 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", "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;
- }
-
-
-
-
-
-
- public static BResult PostFormDataFileThrowException(string url, Stream stream)
- {
- BResult rslt= (BResult)string.Empty;
- using (HttpClient client = CreateClient())
- {
- try
- {
-
- 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);
-
- content.Add(streamContent, "file", "file.zip");
- #endregion
- var tt = client.PostAsync(url, content);
- var result = tt.Result;
- if (result.IsSuccessStatusCode)
- {
- rslt = (HttpResult) result.Content.ReadAsStringAsync().Result;
- }
- else
- {
- throw new Exception(result.ToString());
- }
- }
- catch (Exception e)
- {
- throw new Exception(e.ToString());
- }
- finally
- {
- client.Dispose();
- }
- }
- return rslt;
- }
- }
- }
|