/// <summary> /// 公共方法—— 发送http post请求 2020年6月2日11:20:42 Dennyhui /// </summary> /// <param name="formData">参数</param> /// <param name="requestUri">请求地址</param> /// <param name="token">身份验证秘钥,可为空</param> /// <returns></returns> public async Task<string> Client_Post(MultipartFormDataContent formData, string requestUri, string token) { //从工厂获取请求对象 var client = _httpClientFactory.CreateClient(); //添加请求头 if (!string.IsNullOrWhiteSpace(token)) { client.DefaultRequestHeaders.Add("Authorization", token); } HttpResponseMessage response = client.PostAsync(requestUri, formData).Result; return response.Content.ReadAsStringAsync().Result; } /// <summary> /// 公共方法—— 发送http get 请求 2020年6月2日11:22:11 Dennyhui /// <para>最终以url参数的方式提交</para> /// </summary> /// <param name="parameters">参数字典,可为空</param> /// <param name="requestUri">例如/api/Files/UploadFile</param> /// <returns></returns> public async Task<string> Client_Get(Dictionary<string, string> parameters, string requestUri, string token) { //从工厂获取请求对象 var client = _httpClientFactory.CreateClient(); //添加请求头 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.DefaultRequestHeaders.Add("Content-Type", "application/json; charset=utf-8"); return client.GetStringAsync(requestUri).Result; }
标签:Core,HTTP,string,Get,requestUri,Add,token,client,请求 From: https://www.cnblogs.com/woox/p/17932878.html