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);
            }

  • 相关阅读:
    vim编辑器和bash算术运算入门
    vim编辑器
    egrep及文本处理工具
    grep与基本正则表达式
    bash脚本编程基础及配置文件
    博客开通了
    测试用例考虑因素
    地图测试点的总结
    app测试的case点(2)
    App 99.9%稳定
  • 原文地址:https://www.cnblogs.com/yuesebote/p/10812911.html
Copyright © 2011-2022 走看看