分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

UWP-HttpClient

发布时间:2023-09-06 01:12责任编辑:顾先生关键词:暂无标签
原文:UWP-HttpClient

基本格式:

 ???????????????HttpClient client = new HttpClient(); ???????????????Uri uri = new Uri(url); ???????????????HttpResponseMessage response = await client.GetAsync(uri); ???????????????response.EnsureSuccessStatusCode(); ???????????????string str = await response.Content.ReadAsStringAsync();

若得到Json数据:

  可以通过以下方法方便的转换到类中:

       var jsonMessage = await response.Content.ReadAsStringAsync(); ???????????var serializer = new DataContractJsonSerializer(typeof(JsonClass));//在json2csharp中转换过来的类 ???????????var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonMessage)); ???????????var result = (JsonClass)serializer.ReadObject(ms);

若得到Xml数据:

  可以通过遍历节点将数据一个个手动存放到类中:

string xml = await response.Content.ReadAsStringAsync();if (xml != null){List<CNBlog> list_blogs = new List<CNBlog>();CNBlog cnblog;XmlDocument doc = new XmlDocument();doc.LoadXml(xml);XmlNode feed = doc.ChildNodes[1];foreach (XmlNode node in feed.ChildNodes){if (node.Name.Equals("entry")){cnblog = new CNBlog();foreach (XmlNode node2 in node.ChildNodes){if (node2.Name.Equals("id")){cnblog.ID = node2.InnerText;}if (node2.Name.Equals("title")){cnblog.Title = node2.InnerText;}if (node2.Name.Equals("summary")){cnblog.Summary = node2.InnerText + "...";}}list_blogs.Add(cnblog);}}}

  XML的数据如下:

 ?<?xml version="1.0" encoding="utf-8" ?> - <feed xmlns="http://www.w3.org/2005/Atom"> ?<title type="text">博客园</title> ??<id>uuid:09b99565-46b8-4767-ad8d-ea0283b7b1ac;id=41476</id> ??<updated>2016-05-22T03:01:05Z</updated> ??<link href="http://www.cnblogs.com/" /> - <entry>//一段的入口 ?<id>5516348</id> ??<title type="text">教你如何剖析源码</title> ??<summary type="text">有近千行代码。看着都崩溃了。。。</summary> ??<published>2016-05-22T11:00:00+08:00</published> ??<updated>2016-05-22T03:01:05Z</updated> - <author> ?<name>狼行博客园</name> ??<uri>http://www.cnblogs.com/lang5230/</uri> ??<avatar>http://pic.cnblogs.com/face/835234/20151119122152.png</avatar> ??</author> ?<link rel="alternate" href="http://www.cnblogs.com/lang5230/p/5516348.html" /> ??<blogapp>lang5230</blogapp> ??<diggs>0</diggs> ??<views>1</views> ??<comments>0</comments> ??</entry>

UWP-HttpClient

原文地址:http://www.cnblogs.com/lonelyxmas/p/7559687.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved