调用webApi封装
/// <summary> /// 调用Api(Post请求) /// </summary> /// <param name="url">api地址</param> /// <param name="body">参数</param> /// <returns></returns> public ResponseEntity HttpApiPost(string url, string body) { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; ResponseEntity responseEntity = null; string responseString = string.Empty; try { byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseString = reader.ReadToEnd(); if (!string.IsNullOrEmpty(responseString)) { responseEntity = JsonConvert.DeserializeObject<ResponseEntity>(responseString); } else { LogUtils.error($"调用DB服务(Post请求)返回的消息为空,Url为:{url}"); } } } catch (Exception exception) { LogUtils.error($"调用DB服务(Get请求)发生异常,Url为:{url},返回的消息为:{responseString}", exception); } return responseEntity; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步