您好,欢迎来到钮旅网。
搜索
您的当前位置:首页C#后端调用WEBAPI

C#后端调用WEBAPI

来源:钮旅网

1.调用WEBAPI实现方法

using System.Diagnostics;
using System.Net;

namespace StraightLineSorting.Common
{
    public class CommonFun
    {
        public static async Task<string> dooGet(string url)
        {
            string result = "";
            //  string url = "http://192.168.0.109:8200/GetPatientBySN?SN="+SN;
            //创建HttpClient(注意传入HttpClientHandler)
            var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };

            using (var http = new HttpClient(handler))
            {
                //await异步等待回应
                var response = await http.GetAsync(url);
                //确保HTTP成功状态值
                response.EnsureSuccessStatusCode();

                //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
                Debug.WriteLine(response.Content.ReadAsStringAsync());

                result = await response.Content.ReadAsStringAsync();
            }
            return result;
            Debug.WriteLine("await response.Content.ReadAsStringAsync()");
        }


    }
}

2.调用方法 

        public void getpatientl(string SN)
        { 
            Debug.WriteLine(SN);  
            List<PatientModel> lpm = new List<PatientModel>();
            pm.Barcode = SN;

            string url = "http://192.168.0.109:8200/GetPatientBySN?SN=" + SN;
            string s = CommonFun.dooGet(url).Result;

            Debug.WriteLine(s);
            lpm = JsonConvert.DeserializeObject<List<PatientModel>>(s);
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- niushuan.com 版权所有 赣ICP备2024042780号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务