HttpHelper.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Security.Policy;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using XdCxRhDw.Dto;
  11. namespace XdCxRhDW.Core
  12. {
  13. public class HttpHelper
  14. {
  15. public static async Task<AjaxResult<T>> PostRequestAsync<T>(string url, object dto,int timeoutSeconds=30)
  16. {
  17. var content = new StringContent(JsonConvert.SerializeObject(dto), System.Text.Encoding.UTF8, "application/json");
  18. var handler = new HttpClientHandler() { UseCookies = false };
  19. HttpClient client = new HttpClient(handler);
  20. client.Timeout = TimeSpan.FromSeconds(timeoutSeconds);
  21. var message = new HttpRequestMessage(HttpMethod.Post, url);
  22. message.Content = content;
  23. var response = await client.SendAsync(message);
  24. response.EnsureSuccessStatusCode();
  25. var result = response.Content.ReadAsStringAsync().Result;
  26. var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<T>>(result);
  27. return AjaxResult;
  28. }
  29. /// <summary>
  30. /// 上传文件,wav文件会自动去掉44字节头
  31. /// </summary>
  32. /// <param name="localFile"></param>
  33. /// <param name="uploadUrl"></param>
  34. /// <param name="timeoutSeconds"></param>
  35. /// <returns></returns>
  36. /// <exception cref="Exception"></exception>
  37. public static async Task<string> UploadFileAsync(string localFile, string uploadUrl, int timeoutSeconds = 30)
  38. {
  39. try
  40. {
  41. // 添加文件内容到 MultipartFormDataContent
  42. FileInfo info = new FileInfo(localFile);
  43. bool isWav = info.Extension.ToLower() == ".wav";
  44. byte[] fileBytes = File.ReadAllBytes(localFile);
  45. if (isWav)
  46. {
  47. fileBytes = fileBytes.Skip(44).ToArray();//wav文件有44字节头
  48. }
  49. MultipartFormDataContent content = new MultipartFormDataContent();
  50. ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
  51. content.Add(fileContent, "file", Path.GetFileName(localFile));
  52. var handler = new HttpClientHandler() { UseCookies = false };
  53. HttpClient client = new HttpClient(handler);
  54. client.Timeout = TimeSpan.FromSeconds(timeoutSeconds);
  55. var message = new HttpRequestMessage(HttpMethod.Post, uploadUrl);
  56. message.Content = content;
  57. var response = await client.SendAsync(message);
  58. response.EnsureSuccessStatusCode();
  59. var result = await response.Content.ReadAsStringAsync();
  60. var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<FileUploadResDto>>(result);
  61. if (AjaxResult.code == 200)
  62. {
  63. return AjaxResult.data.FileName;
  64. }
  65. else
  66. {
  67. throw new Exception(AjaxResult.msg);
  68. }
  69. }
  70. catch
  71. {
  72. throw new Exception($"上传文件{Path.GetFileName(localFile)}到{uploadUrl}失败!");
  73. }
  74. }
  75. }
  76. }