zoukankan      html  css  js  c++  java
  • Winform中调用WebApi并解析返回数据

     private async void button1_Click(object sender, EventArgs e)
            {
              
               DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HardwareStatusBase));
                MemoryStream ms = new MemoryStream();
                //将资料写入MemoryStream
                serializer.WriteObject(ms, new HardwareStatusBase()
                {
                    IP = "192.168.0.13",
                    port = "8081",
                    Devicecode = "BB7584",
                    DeviceName = "报警器",
                    DeviceStatuss = 1,
                    message = "设备打开",
                    operation = 1
                });
                //一定要在这设定Position
                ms.Position = 0;
                HttpContent content = new StreamContent(ms);//将MemoryStream转成HttpContent
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpClient client = new HttpClient();
                //由HttpClient发出Put Method
                HttpResponseMessage response = await client.PostAsync("http://192.168.0.11:8011/HardwareStatus/HardwareStatus", content);
                string result = await response.Content.ReadAsStringAsync();//将调用API返回数据转换为字符串
                if (response.IsSuccessStatusCode)
                    MessageBox.Show(result);
            }

  • 相关阅读:
    ajax请求
    easyui相关问题
    linux解压命令
    angular2 中文学习资料整理
    在angular2服务中注入服务
    Angular 2模板语法
    登录,注册流程(基于token的身份验证)
    Node Js与JavaScript的区别及nodejs优缺点。
    yarn包管理网站
    浅谈Cookie,Session,WebStorage区别,应用场景
  • 原文地址:https://www.cnblogs.com/yuesebote/p/10812911.html
Copyright © 2011-2022 走看看