| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using dodohold.core;
- using Dataoke;
- using System.Net.Http.Headers;
- using Microsoft.AspNetCore.Http;
- using System.Net.Http;
- using System.Net.Mime;
- using static dodohold.core.ZTOExpress.CreateOrderArgs;
- using System.Net;
- using System.Text;
- namespace molilian.core
- {
- public class ImageData
- {
- public string url { get; set; } = string.Empty;
- public string filename { get; set; } = string.Empty;
- public object fileType { get; set; } = false;
- public int fileSize { get; set; } = 0;
- }
- public class ImageRootData
- {
- public bool success { get; set; } = false;
- public int code { get; set; } = 200;
- public ImageData? data { get; set; }
- public string message { get; set; } = string.Empty;
- }
- public partial class ImageHostingPlus
- {
- public static async Task<ImageRootData> Dianping(string file_name, byte[] bytes)
- {
- var ts = DateTime.Now.Convert2UnixTimestamp(true).ToString();
- Guid guid = Guid.NewGuid();
- string randomString = guid.ToString();
- var fileContent = new ByteArrayContent(bytes)
- {
- Headers =
- {
- ContentDisposition = new ContentDispositionHeaderValue("form-data")
- {
- Name = "files",
- FileName = "blob",
- }
- }
- };
- var exts = file_name.GetLastContentPart(".", "");
- var contentType = WebClientUtility.GetContentType(exts);
- fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
- var formData = new MultipartFormDataContent()
- {
- { new StringContent(file_name), "fileName"},
- { new StringContent(ts), "fileID"},
- { new StringContent("0"),"part"},
- { new StringContent("1"), "partSize"},
- { fileContent, "files"}
- };
- // 创建 HttpClient
- using HttpClient client = new();
- client.DefaultRequestHeaders.Referrer = new Uri("https://h5.dianping.com/");
- //client.DefaultRequestHeaders.Add("CSC-VisitId", $"access-{randomString}");
- client.DefaultRequestHeaders.Add("CSC-VisitId", $"access-11514014-0606-4887-8251-988914b16724");
- 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");
- // 设置上传的服务器地址
- string uploadUrl = "https://kf.dianping.com/api/file/burstUploadFile";
- // 构建 HTTP 请求
- HttpRequestMessage request = new(HttpMethod.Post, uploadUrl);
- request.Content = formData;
- // 发送 POST 请求上传文件
- HttpResponseMessage response = await client.SendAsync(request);
- // 读取响应内容
- string responseBody = await response.Content.ReadAsStringAsync();
- var root = responseBody.Convert2JsonElement();
- var result = new ImageRootData();
- result.success = root.Read<bool>("success", false);
- if (result.success)
- {
- _ = new LoggerLibrary("ImageHosting", "meituan").Info(responseBody).SaveAsync();
- }
- result.code = root.Read<int>("code", 0);
- result.message = root.Read<string>("errMsg", string.Empty);
- result.data = new ImageData
- {
- url = root.PathRead<string>("data.uploadPath", string.Empty),
- filename = root.PathRead<string>("data.filename", string.Empty),
- fileSize = root.PathRead<int>("data.fileSize", 0),
- fileType = contentType
- };
- int fileSize = root.PathRead<int>("data.fileSize");
- return result;
- }
- }
- }
|