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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值