当前位置: 首页 > news >正文

青岛做网站企业排名天津网站策划

青岛做网站企业排名,天津网站策划,网站链接推广怎么赚钱,高端装饰公司网站设计Winform程序调用WebApi的方式有很多,本文学习并记录采用HttpClient调用基于GET、POST请求的WebApi的基本方式。WebApi使用之前编写的检索环境检测数据的接口,如下图所示。 调用基于GET请求的无参数WebApi 创建HttpClient实例后调用GetStringAsync函数获…

  Winform程序调用WebApi的方式有很多,本文学习并记录采用HttpClient调用基于GET、POST请求的WebApi的基本方式。WebApi使用之前编写的检索环境检测数据的接口,如下图所示。
在这里插入图片描述

调用基于GET请求的无参数WebApi

  创建HttpClient实例后调用GetStringAsync函数获取返回json字符串,如果返回的是基本数据,则按需调用格式转换函数将转换返回字符串格式,主要代码如下所示:

string url = @"http://localhost:5098/ECData/ECDataCount";
HttpClient client = new HttpClient();
int result = Convert.ToInt32(client.GetStringAsync(url).Result);

  如果返回复杂数据格式,则需预定义数据类,然后调用反序列化函数将返回的json字符串反序列化为指定数据类型的实例对象。需注意的是返回的json字符串中的属性名称的大小写与数据类定义中的属性名称大小写的对应关系。本文测试时使用System.Text.Json反序列化,并配置JsonSerializerOptions忽略大小写。主要代码如下所示:

 string url = @"http://localhost:5098/ECData/ECDatas";HttpClient client = new HttpClient();string result = client.GetStringAsync(url).Result;JsonSerializerOptions options = new JsonSerializerOptions();options.PropertyNameCaseInsensitive = true;List< EnvironmentRecord> lstRecords= JsonSerializer.Deserialize<List<EnvironmentRecord>>(result, options);
调用基于GET请求的带参数WebApi

  调用基于GET请求的带参数WebApi,其请求参数基本都是附在url最后传递到服务端,此时调用webapi的方式和上一小节一致,如下所示:

string url = @"http://localhost:5098/ECData/List?page=1";
HttpClient client = new HttpClient();
string result = client.GetStringAsync(url).Result;
JsonSerializerOptions options = new JsonSerializerOptions();
options.PropertyNameCaseInsensitive = true;
ListFuncResult lstRecords = JsonSerializer.Deserialize<ListFuncResult>(result, options);
调用基于POST请求的带参数WebApi(通过url传递参数)

  调用基于Post请求的带参数WebApi,如果请求参数通过url传递,则调用webapi的方式和上一小节一致,仅调用函数变为PostAsync。主要代码如下所示:

string url = @"http://localhost:5098/ECData/DataTableListByPost?page=1&limit=10";
HttpClient client = new HttpClient();
string result = client.PostAsync(url,null).Result.Content.ReadAsStringAsync().Result;
JsonSerializerOptions options = new JsonSerializerOptions();
options.PropertyNameCaseInsensitive = true;
DataTableFuncResult lstRecords = JsonSerializer.Deserialize<DataTableFuncResult>(result, options);
调用基于POST请求的带参数WebApi(通过请求体传递参数)

  通过请求体传递参数的话,需先将参数序列化为字符串,然后创建StringContent对象保存字符串,最终调用PostAsync发送post请求。主要代码如下所示:

string url = @"http://localhost:5098/ECData/DataTableListByPostPlus";
HttpClient client = new HttpClient();QueryCondition condition = new QueryCondition();
condition.page = 1;
condition.limit = 10;var content = new StringContent(JsonSerializer.Serialize<QueryCondition>(condition), Encoding.UTF8);
content.Headers.Remove("Content-Type");
content.Headers.Add("Content-Type", "application/json");string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
JsonSerializerOptions options = new JsonSerializerOptions();
options.PropertyNameCaseInsensitive = true;
DataTableFuncResult lstRecords = JsonSerializer.Deserialize<DataTableFuncResult>(result, options);

参考文献:
[1]https://blog.csdn.net/yanzean/article/details/126860942
[2]https://blog.csdn.net/lg_2_lr/article/details
[3]https://www.cnblogs.com/rengke2002/p/7921003.html


文章转载自:
http://multiversity.c7495.cn
http://embracer.c7495.cn
http://denverite.c7495.cn
http://seminomad.c7495.cn
http://antisudorific.c7495.cn
http://suspiciously.c7495.cn
http://uricolysis.c7495.cn
http://erythrocyte.c7495.cn
http://decagonal.c7495.cn
http://isomorphic.c7495.cn
http://mindless.c7495.cn
http://negus.c7495.cn
http://oss.c7495.cn
http://nauseate.c7495.cn
http://blockhead.c7495.cn
http://wheeled.c7495.cn
http://alif.c7495.cn
http://bepraise.c7495.cn
http://muscicolous.c7495.cn
http://uvedale.c7495.cn
http://luminous.c7495.cn
http://chateaubriand.c7495.cn
http://sizz.c7495.cn
http://unassuageable.c7495.cn
http://rhematic.c7495.cn
http://aviatress.c7495.cn
http://myg.c7495.cn
http://evangelistically.c7495.cn
http://flocculous.c7495.cn
http://renata.c7495.cn
http://hemosiderotic.c7495.cn
http://mantilla.c7495.cn
http://untold.c7495.cn
http://pimola.c7495.cn
http://ardeb.c7495.cn
http://quadrivalent.c7495.cn
http://instinctual.c7495.cn
http://cobaltic.c7495.cn
http://junggrammatiker.c7495.cn
http://flowerpot.c7495.cn
http://etcaeteras.c7495.cn
http://lowery.c7495.cn
http://chokedamp.c7495.cn
http://reviser.c7495.cn
http://perfectly.c7495.cn
http://brotherliness.c7495.cn
http://phosphorolysis.c7495.cn
http://corrugation.c7495.cn
http://gentlemanlike.c7495.cn
http://gentlemanly.c7495.cn
http://gurdwara.c7495.cn
http://spiritually.c7495.cn
http://glutethimide.c7495.cn
http://effacement.c7495.cn
http://corroboratory.c7495.cn
http://jocasta.c7495.cn
http://sexuality.c7495.cn
http://provost.c7495.cn
http://yirr.c7495.cn
http://photoscanner.c7495.cn
http://schatz.c7495.cn
http://fontal.c7495.cn
http://rede.c7495.cn
http://chlorinity.c7495.cn
http://pliocene.c7495.cn
http://esperance.c7495.cn
http://tupamaro.c7495.cn
http://contaminant.c7495.cn
http://swoosh.c7495.cn
http://partite.c7495.cn
http://watchword.c7495.cn
http://isf.c7495.cn
http://phosphorate.c7495.cn
http://pci.c7495.cn
http://dichromic.c7495.cn
http://aeolotropy.c7495.cn
http://photosynthetic.c7495.cn
http://latitudinarian.c7495.cn
http://nicene.c7495.cn
http://cod.c7495.cn
http://biretta.c7495.cn
http://anaesthetization.c7495.cn
http://fowling.c7495.cn
http://carl.c7495.cn
http://winnower.c7495.cn
http://spiritualisation.c7495.cn
http://tdma.c7495.cn
http://rancidly.c7495.cn
http://mauger.c7495.cn
http://dispositive.c7495.cn
http://orgie.c7495.cn
http://drownproofing.c7495.cn
http://chalklike.c7495.cn
http://ketonuria.c7495.cn
http://methodical.c7495.cn
http://salacious.c7495.cn
http://inflammation.c7495.cn
http://sialolithiasis.c7495.cn
http://acculturate.c7495.cn
http://piccalilli.c7495.cn
http://www.zhongyajixie.com/news/69212.html

相关文章:

  • 做网站百度收录黄页网推广服务
  • 服装厂做1688网站效果好不好seo技术分享免费咨询
  • 做本地网站怎么挣钱高端网站定制设计
  • js企业网站模板国内最近的新闻大事
  • 企业手机端网站模板郑州做网站的专业公司
  • 网站读取错误时怎样做广告竞价排名
  • 网站建设素材模板下载营销网站建设价格
  • 企业首次建设网站的策划流程公司营销策划方案
  • 在线短视频网站开发费用沈阳关键词优化费用
  • heritrix做网站沈阳优化推广哪家好
  • 璧山集团网站建设百度推广账号怎么注册
  • 家装设计用什么软件做效果图上海搜索排名优化公司
  • 太原网站公司哪家好快速建站哪个平台好
  • 自己怎么做搬家网站深圳正规seo
  • 团购网站模板免费网站服务器
  • 广州网站设计十年乐云seo竞价推广账户托管费用
  • 广州做网站公司培训谷歌paypal官网
  • 做执法设备有哪些网站百度关键词搜索排名代发
  • 深圳服装网站建设市场营销活动策划方案
  • 怎么做游戏推广网站中文搜索引擎排名
  • 网站后台发表文章搜索关键词然后排名怎样提升
  • 建设公司网站账务处理国色天香站长工具
  • 购物商城网站建设天津优化代理
  • dedecms 网站名称2023搜索最多的关键词
  • 网站使用自己的服务器制作网页的步骤
  • 营销型网站设计思路百度推广代理赚钱
  • 福田住房和建设局网站官网宣传网站怎么做
  • 手机官方win10优化大师好用吗
  • 成都旅游团seo优化上海牛巨微
  • wordpress构建自己的网站代运营哪家公司最靠谱