ImageHostingPlus.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using dodohold.core;
  2. using Dataoke;
  3. using System.Net.Http.Headers;
  4. using Microsoft.AspNetCore.Http;
  5. using System.Net.Http;
  6. using System.Net.Mime;
  7. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  8. using System.Net;
  9. using System.Text;
  10. namespace molilian.core
  11. {
  12. public class ImageData
  13. {
  14. public string url { get; set; } = string.Empty;
  15. public string filename { get; set; } = string.Empty;
  16. public object fileType { get; set; } = false;
  17. public int fileSize { get; set; } = 0;
  18. }
  19. public class ImageRootData
  20. {
  21. public bool success { get; set; } = false;
  22. public int code { get; set; } = 200;
  23. public ImageData? data { get; set; }
  24. public string message { get; set; } = string.Empty;
  25. }
  26. public partial class ImageHostingPlus
  27. {
  28. public static async Task<ImageRootData> Dianping(string file_name, byte[] bytes)
  29. {
  30. var ts = DateTime.Now.Convert2UnixTimestamp(true).ToString();
  31. Guid guid = Guid.NewGuid();
  32. string randomString = guid.ToString();
  33. var fileContent = new ByteArrayContent(bytes)
  34. {
  35. Headers =
  36. {
  37. ContentDisposition = new ContentDispositionHeaderValue("form-data")
  38. {
  39. Name = "files",
  40. FileName = "blob",
  41. }
  42. }
  43. };
  44. var exts = file_name.GetLastContentPart(".", "");
  45. var contentType = WebClientUtility.GetContentType(exts);
  46. fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
  47. var formData = new MultipartFormDataContent()
  48. {
  49. { new StringContent(file_name), "fileName"},
  50. { new StringContent(ts), "fileID"},
  51. { new StringContent("0"),"part"},
  52. { new StringContent("1"), "partSize"},
  53. { fileContent, "files"}
  54. };
  55. // 创建 HttpClient
  56. using HttpClient client = new();
  57. client.DefaultRequestHeaders.Referrer = new Uri("https://h5.dianping.com/");
  58. //client.DefaultRequestHeaders.Add("CSC-VisitId", $"access-{randomString}");
  59. client.DefaultRequestHeaders.Add("CSC-VisitId", $"access-11514014-0606-4887-8251-988914b16724");
  60. client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36");
  61. // 设置上传的服务器地址
  62. string uploadUrl = "https://kf.dianping.com/api/file/burstUploadFile";
  63. // 构建 HTTP 请求
  64. HttpRequestMessage request = new(HttpMethod.Post, uploadUrl);
  65. request.Content = formData;
  66. // 发送 POST 请求上传文件
  67. HttpResponseMessage response = await client.SendAsync(request);
  68. // 读取响应内容
  69. string responseBody = await response.Content.ReadAsStringAsync();
  70. var root = responseBody.Convert2JsonElement();
  71. var result = new ImageRootData();
  72. result.success = root.Read<bool>("success", false);
  73. if (result.success)
  74. {
  75. _ = new LoggerLibrary("ImageHosting", "meituan").Info(responseBody).SaveAsync();
  76. }
  77. result.code = root.Read<int>("code", 0);
  78. result.message = root.Read<string>("errMsg", string.Empty);
  79. result.data = new ImageData
  80. {
  81. url = root.PathRead<string>("data.uploadPath", string.Empty),
  82. filename = root.PathRead<string>("data.filename", string.Empty),
  83. fileSize = root.PathRead<int>("data.fileSize", 0),
  84. fileType = contentType
  85. };
  86. int fileSize = root.PathRead<int>("data.fileSize");
  87. return result;
  88. }
  89. }
  90. }