using Common.Extensions; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace Common.Tools { /// /// 上传文件实体 /// public class PostFileListEntity { /// /// 文件数据流 /// public System.IO.Stream FileValue { get; set; } /// /// 文件名称 包含拓展名 /// public string FileName { get; set; } /// /// 拓展名 /// public string GetExtension { get { return FileName.Split('.').Length == 2? FileName.Split('.')[1]:""; } } } public enum PsotType { /// /// 提交Json数据模式 application/json /// [EnumText("提交Json数据模式 application/json")] Json = 1, /// /// 提交FormData multipart/form-data表单数据模式 /// [EnumText("提交表单数据模式 multipart/form-data")] Form = 2, /// /// 提交application/x-www-form-urlencoded模式提交 /// [EnumText("提交表单数据模型 application/x-www-form-urlencoded")] Urlencoded=3 } /// /// Http操作类型 post get /// public class HttpHelp { #region Post /// /// Post请求 /// /// Json时用对象, form模式用 Dictionary form支持 a=1&b=2字符串和对象模式 /// 请求地址 /// 提交数据模式 默认json模式 /// 提交的文件数据流 /// token /// 超时时间 秒 默认300 /// 提交的Json数据序列化是否和model一致 True 时候会采用小驼峰格式方式提交 False 对象原始大小写格式 /// public static async Task Post(T PostData, string RequestUrl,PsotType postType= PsotType.Json,List FileList=null, string Token = null,int TimeOut=3000,bool JsonNotWebOption = true) { var html = ""; try { switch (postType) { case PsotType.Json: html = await PostJson(PostData, RequestUrl, Token,TimeOut, JsonNotWebOption: JsonNotWebOption); break; case PsotType.Form: html = await PostForm(_FormDataFormat(PostData), RequestUrl, FileList, Token,TimeOut); break; case PsotType.Urlencoded: html = await PostFormUrlencoded(_FormDataFormat(PostData),RequestUrl,Token,TimeOut); break; } } catch { html = null; } return html; } /// /// 格式化formdata数据字典 /// /// /// private static Dictionary _FormDataFormat(object PostData) { var formdata = new Dictionary(); if (typeof(string) == PostData.GetType()) { foreach (var item in PostData.ToString().Split("&")) { var val = item.Split("="); if (val.Length == 2) { formdata.Add(val[0], val[1]); } } } else if (typeof(Dictionary) == PostData.GetType()) { formdata = (Dictionary)PostData; } else { foreach (var item in PostData.GetType().GetProperties()) { var val = item.GetValue(PostData); if (val != null && val.ToString().IsNotNull()) { formdata.Add(item.Name, val.ToString()); } } } return formdata; } /// /// Post 表单方式请求数据 /// /// /// /// /// 超时时间 秒 /// public static async Task PostFormUrlencoded(Dictionary FormData, string RequestUrl ,string Token = null,int TimeOut=1200) { var client = new HttpClient(); try { //添加请求头 if (!string.IsNullOrWhiteSpace(Token)) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Token); } client.DefaultRequestHeaders.Accept.Clear(); var dataurl = ""; foreach (var item in FormData) { dataurl += $"{item.Key}={item.Value}&"; } var content = new StringContent(dataurl); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); client.Timeout = new TimeSpan(0, 0, TimeOut); var response = await client.PostAsync(RequestUrl, content); var html = await response.Content.ReadAsStringAsync(); client.Dispose(); response.Dispose(); return html; } catch (Exception ex) { var message = ex.Message; return null; } } /// /// Post 表单方式请求数据 /// /// /// /// 上传的文件列表 /// /// 超时时间 秒 /// public static async Task PostForm(Dictionary FormData, string RequestUrl, List FileList = null, string Token = null, int TimeOut = 1200) { var client = new HttpClient(); try { //添加请求头 if (!string.IsNullOrWhiteSpace(Token)) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Token); } client.DefaultRequestHeaders.Accept.Clear(); var content = new MultipartFormDataContent(); foreach (var item in FormData) { content.Add(new StringContent(item.Value), item.Key); } if (FileList != null) { foreach (var item in FileList) { content.Add(new StreamContent(item.FileValue, (int)item.FileValue.Length), "file", item.FileName); } } client.Timeout = new TimeSpan(0, 0, TimeOut); var response = await client.PostAsync(RequestUrl, content); var html = await response.Content.ReadAsStringAsync(); client.Dispose(); response.Dispose(); return html; } catch (Exception ex) { var message = ex.Message; return null; } } /// /// Post请求提交Json格式数据 /// /// /// /// /// /// 请求超时时间 /// True 时候会采用小驼峰格式方式提交 False 对象原始大小写格式 /// options public static async Task PostJson(T DataDto, string requestUri, string token=null, int TimeOut = 1200, bool JsonNotWebOption = true) { var client = new HttpClient(); try { //添加请求头 if (!string.IsNullOrWhiteSpace(token)) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); } client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpContent httpContent = JsonContent.Create(DataDto); if (JsonNotWebOption) {//json序列化格式 默认按照 定义的属性 httpContent = JsonContent.Create( DataDto,options:new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); } httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); client.Timeout = new TimeSpan(0,0,TimeOut); HttpResponseMessage response = await client.PostAsync(requestUri, httpContent); var html = await response.Content.ReadAsStringAsync(); client.Dispose(); response.Dispose(); return html; } catch { return null; } } #endregion /// /// 发送Get请求 /// /// /// /// /// /// 请求超时时间 秒 默认1200秒 /// public static async Task Get(T Getdata, string requestUri, string token=null,int TimeOut=1200) { var parameters = new Dictionary(); foreach (var item in typeof(T).GetProperties()) { var value = item.GetValue(Getdata); if (value != null) { parameters.Add(item.Name,value.ToString()); } } return await Get(parameters, requestUri, token); } /// /// 公共方法—— 发送http get 请求 2020年6月2日11:22:11 Dennyhui /// 最终以url参数的方式提交 /// /// 参数字典,可为空 /// 例如/api/Files/UploadFile /// 请求超时时间 秒 /// public static async Task Get(Dictionary parameters, string requestUri, string token=null,int TimeOut=1200) { //从工厂获取请求对象 var client = new HttpClient(); //添加请求头 if (!string.IsNullOrWhiteSpace(token)) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); } //拼接地址 if (parameters != null) { var strParam = string.Join("&", parameters.Select(o => o.Key + "=" + o.Value)); requestUri = string.Concat(requestUri, '?', strParam); } client.BaseAddress = new Uri(requestUri); client.Timeout = new TimeSpan(0,0, TimeOut); var html= await client.GetStringAsync(requestUri); client.Dispose(); return html; } /// /// 获取下载http远程文件流 /// /// /// public static async Task GetUrlFileStream(string FilepathUrl) { try { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(FilepathUrl); var stream = await client.GetStreamAsync(FilepathUrl); //byte[] bytes = new byte[stream.Length]; // stream.Read(bytes, 0, bytes.Length); // return bytes; int _fileream = stream.ReadByte(); List _btlist = new List(); while (_fileream > -1) { _btlist.Add((byte)_fileream); _fileream = stream.ReadByte(); } byte[] filebyte = _btlist.ToArray(); return filebyte; } catch { return null; } } /// /// 下载远程文件到本地 /// /// /// 本地存储地址 /// 保存的文件名称 /// public static async Task FileDown(string Fileurl,string SaveFilePath,string SaveFileName) { if (SaveFileName.IsNull()) { if (Fileurl.IndexOf('\\') >= 0) { var fl = Fileurl.Split('\\'); SaveFileName = fl[fl.Length - 1]; } else { var fl = Fileurl.Split('/'); SaveFileName = fl[fl.Length - 1]; } } var Filestream=await GetUrlFileStream(Fileurl); if (Filestream != null) { Directory.CreateDirectory(SaveFilePath); SaveFileName = SaveFilePath + "/" + SaveFileName; using (FileStream filestream = File.Create(SaveFileName)) { filestream.Write(Filestream); filestream.Close(); } } else { SaveFileName = null; } return SaveFileName; } } }