zoukankan      html  css  js  c++  java
  • 调用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;
            }
  • 相关阅读:
    日记2014/06/25
    Cocos2dx 3.1.1 学习笔记整理(4):事件监听与Action的初步使用
    Cocos2dx 3.1.1 学习笔记整理(3):逐帧动画
    Cocos2dx 3.1.1 学习笔记整理(2):创建场景与载入图片
    Cocos2dx 3.1.1 学习笔记整理(1) 新建项目
    5-20
    5-19
    5-18
    5-17
    5-16
  • 原文地址:https://www.cnblogs.com/yuanshuo/p/14690902.html
Copyright © 2011-2022 走看看