• Winfrom调用WebApi获取数据


    1、get或者POST传输一个小于255字节方式

    传递小型数据量的信息到服务端去获取服务器信息的方法

     /// <summary>
            /// 服务端源获取信息
            /// </summary>
            /// <param name="strUrl">服务URL</param>
            /// <param name="objParemeters">参数列表(实体型)</param>
            /// <param name="ticike">身份号</param>
            /// <returns>返回结果实体</returns>
            public static T JsonRequestToObject<T>(string strUrl, Object objParemeters, string ticike)
            {
                try
                {
                    Postinfo postinfo = new Postinfo();
                    postinfo.ticike = ticike;
                    postinfo.content = JsonConvert.SerializeObject(objParemeters);
                    postinfo.Client = "PcClient";
                    string param = JsonConvert.SerializeObject(postinfo);
    
                    using (var client = new HttpClient())
                    {
                        var content = new FormUrlEncodedContent(new Dictionary<string, string>()       
                        {
                           {"", param}//键名必须为空
                        });
                        var result = client.PostAsync(strUrl, content).Result.Content.ReadAsStringAsync().Result;
                        T ds = JsonConvert.DeserializeObject<T>(result);
                        return ds;
                    }
    
                }
                catch (Exception ex)
                {
                    //Log.Writer(ex.Message + ":" + ex.StackTrace);
                    LogHelper.WriteLog(typeof(T), ex.StackTrace);
                    object obj = Activator.CreateInstance(typeof(T));
                    return (T)obj;
                }
            }
    View Code

    因为WebAPi传递信息需要一个特殊的FromBody来接收,需要用个类型来包信息后webapi才能获取到传递来的信息

    var content = new FormUrlEncodedContent(new Dictionary<string, string>()       
                        {
                           {"", param}//键名必须为空
                        });

    WebApi定义的接受方式

    [HttpPost]
            public HttpResponseMessage UserLogin([FromBody]string postinfo)
            {
                  //逻辑代码
                  XXXXXXXXXX
           }

    [FromBody]获取到的信息类型为一个string 

    2、POS传输一个大于255字节方式

    当你需要传递个大型数据时(例如分片上传文件时)

    /// <summary>
            /// 像服务器传输一个超过255字节的大数据,服务端源获取返回一个实体
            /// </summary>
            /// <param name="strUrl">服务URL</param>
            /// <param name="objParemeters">参数列表(实体型)</param>
            /// <param name="ticike">身份号</param>
            /// <returns>返回结果实体</returns>
            public static T JsonRequestServiceToObject<T>(string strUrl, Object objParemeters, string ticike)
            {
                try
                {
                    Postinfo postinfo = new Postinfo();
                    postinfo.ticike = ticike;
                    postinfo.content = JsonConvert.SerializeObject(objParemeters);
                    postinfo.Client = "PcClient";
                    string param = JsonConvert.SerializeObject(postinfo);
    
                    using (var client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        var httpContent = new StringContent(param, Encoding.UTF8);
                        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        var result = client.PostAsync(strUrl, httpContent).Result.Content.ReadAsStringAsync().Result;
                        T ds = JsonConvert.DeserializeObject<T>(result);
                        return ds;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(typeof(T), ex);
                    object obj = Activator.CreateInstance(typeof(T));
                    return (T)obj;
                }
            }
    View Code

    改变的是包住信息的类型

     var httpContent = new StringContent(param, Encoding.UTF8);
    View Code

    webApi获取的信息的方式改为用个定义的类来获取

    [HttpPost]
    public HttpResponseMessage PostUploadFile([FromBody]Postinfo postinfo)
    {
      //逻辑代码
      XXXXXXXXX  
    }
    View Code
  • 相关阅读:
    swoole学习(四):websocket
    LeetCode--SQL 查询:体育馆的人流量
    LeetCode--SQL 查询:有趣的电影
    centos7下mysql5.7忘记密码
    LeetCode--SQL 查询:删除重复的电子邮箱。
    swoole学习(三):HTTP服务
    swoole学习(二):TCP/UDP服务器-客户端
    swoole学习(一):初识swoole
    LeetCode--SQL 查询:查找部门工资前三高的职员
    报文、报文段、分组、包、数据报、帧、数据流的概念区别
  • 原文地址:https://www.cnblogs.com/hyxf/p/5251656.html
Copyright © 2020-2023  润新知