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

  • 相关阅读:
    JDK、JRE、JVM的区别联系
    1.1 计算机基础知识 —— 二进制
    java--面试中遇到的各种比较区别
    springMVC 运行流程
    算法--常用排序和查找
    Java面试问题总结
    dpkg:error错误求解:——重复文件触发关注
    memcpy实现
    Palindrome Number
    no matching function for call to 'sort(...),
  • 原文地址:https://www.cnblogs.com/yuesebote/p/10812911.html
Copyright © 2011-2022 走看看