using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System.Text; namespace AmrControl.Common.HttpClients { /// /// post请求客户端 /// public class HttpPost : BaseHttpClient { /// /// 设置传输类型,请在请求时注意类型, /// private MediaType mediaType = MediaType.APPLICATION_JSON_VALUE; public HttpPost(HttpClient httpClient, string baseUri) : base(httpClient, baseUri) { } public void setMediaType(MediaType type) { this.mediaType = type; } protected override HttpMethod getMethod() { return HttpMethod.Post; } protected override HttpContent? handleData(T data) { if(mediaType == MediaType.APPLICATION_JSON_VALUE) { //请求内容在请求体中时使用 return new StringContent(JsonConvert.SerializeObject(data, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }), Encoding.UTF8, "application/json"); } else { return new FormUrlEncodedContent(convertObj2Dict(data)); } } /// /// 下载文件 /// /// /// /// /// public HttpRespResult downloadFile(string url, string filePath, T data) { return downloadFile(url, filePath, new(), data); } /// /// 下载文件 /// /// /// /// /// /// public HttpRespResult downloadFile(string url, string filePath, Dictionary headers, T data) { setMediaType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); var response = _httpClient.SendAsync(createRequest(reHandleUrl(url, data), headers, getMethod(), handleData(data))).Result; using var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); response.Content.CopyToAsync(fs); return handleResult(response, filePath); } /// /// 下载文件 /// /// /// /// /// public HttpRespResult downloadFileForJson(string url, string filePath, T data) { return downloadFileForJson(url, filePath, new(), data); } /// /// 下载文件 /// /// /// /// /// /// public HttpRespResult downloadFileForJson(string url, string filePath, Dictionary headers, T data) { setMediaType(MediaType.APPLICATION_JSON_VALUE); var response = _httpClient.SendAsync(createRequest(reHandleUrl(url, data), headers, getMethod(), handleData(data))).Result; using var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); response.Content.CopyToAsync(fs); return handleResult(response, filePath); } /// /// 上传文件 /// /// /// /// /// public HttpRespResult uploadFile(string url, T? data, string filePath) { if (string.IsNullOrEmpty(filePath)) { return sendRequest(url, data); } return uploadFile(url, new(), data, filePath); } /// /// 上传文件 /// /// /// /// /// /// public HttpRespResult uploadFile(string url, Dictionary headers, T? data, string filePath) { if (string.IsNullOrEmpty(filePath)) { return sendRequest(url, headers, data); } var content = new MultipartFormDataContent(); if (data != null) { Dictionary dict = convertObj2Dict(data); foreach (var item in dict) { if (string.IsNullOrEmpty(item.Value) || string.IsNullOrWhiteSpace(item.Value)) { continue; } content.Add(new StringContent(item.Value), item.Key); } } content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)), "file", filePath.IndexOf("/") > 0 ? filePath.Substring(filePath.LastIndexOf("/")) : filePath); setMediaType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); var response = _httpClient.SendAsync(createRequest(reHandleUrl(url, data), headers, HttpMethod.Post, content)).Result; var rs = response.Content.ReadAsStringAsync().Result; return handleResult(response, rs); } } }