| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 | 
							- using Newtonsoft.Json;
 
- using System;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Net.Http;
 
- using System.Security.Policy;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- using XdCxRhDw.Dto;
 
- using XdCxRhDW.Repostory.EFContext;
 
- namespace XdCxRhDW.Core
 
- {
 
-     public class HttpHelper
 
-     {
 
-         /// <summary>
 
-         /// 
 
-         /// </summary>
 
-         /// <typeparam name="T"></typeparam>
 
-         /// <param name="url"></param>
 
-         /// <param name="dto">dto对象</param>
 
-         /// <returns></returns>
 
-         public static T PostRequest<T>(string url, object dto)
 
-         {
 
-             var content = new StringContent(JsonConvert.SerializeObject(dto), System.Text.Encoding.UTF8, "application/json");
 
-             var handler = new HttpClientHandler() { UseCookies = false };
 
-             HttpClient client = new HttpClient(handler);
 
-             client.Timeout = TimeSpan.FromSeconds(20);
 
-             var message = new HttpRequestMessage(HttpMethod.Post, url);
 
-             message.Content = content;
 
-             var response = client.SendAsync(message).Result;
 
-             response.EnsureSuccessStatusCode();
 
-             var result = response.Content.ReadAsStringAsync().Result;
 
-             var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<T>>(result);
 
-             if (AjaxResult.code == 200)
 
-             {
 
-                 return AjaxResult.data;
 
-             }
 
-             else
 
-             {
 
-                 throw new Exception(AjaxResult.msg);
 
-             }
 
-         }
 
-         public static string UploadFile(string localFile, string uploadUrl)
 
-         {
 
-             try
 
-             {
 
-                 // 添加文件内容到 MultipartFormDataContent
 
-                 byte[] fileBytes = File.ReadAllBytes(localFile);
 
-                 MultipartFormDataContent content = new MultipartFormDataContent();
 
-                 ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
 
-                 content.Add(fileContent, "file", Path.GetFileName(localFile));
 
-                 var handler = new HttpClientHandler() { UseCookies = false };
 
-                 HttpClient client = new HttpClient(handler);
 
-                 client.Timeout = TimeSpan.FromSeconds(20);
 
-                 var message = new HttpRequestMessage(HttpMethod.Post, uploadUrl);
 
-                 message.Content = content;
 
-                 var response = client.SendAsync(message).Result;
 
-                 response.EnsureSuccessStatusCode();
 
-                 var result = response.Content.ReadAsStringAsync().Result;
 
-                 var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<FileUploadResDto>>(result);
 
-                 if (AjaxResult.code == 200)
 
-                 {
 
-                     return AjaxResult.data.FileName;
 
-                 }
 
-                 else
 
-                 {
 
-                     throw new Exception(AjaxResult.msg);
 
-                 }
 
-             }
 
-             catch
 
-             {
 
-                 throw new Exception($"上传文件{Path.GetFileName(localFile)}到{uploadUrl}失败!");
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |