C#后端调用WEBAPI
后台调用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);
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)