Fork me on GitHub

UWP 使用HttpClient获取网页数据

我的App自然灾害中,为了展示地震的各种信息,就需要从网页上获取地震源数据。

 

如图所示,我们需要展示 地震等级、地震发生时间、经纬度、震源深度、地震位置等信息。

 

 

那么,假设给了一个地震的源,中国地震台网——————http://1.eqweixin.sinaapp.com/zxml.php

怎么让程序来获取里面的内容呢/?

如果你在浏览器中打开上面的连接,那么将展示的一段xml数据

 

 

 在UWP程序里面,我们可以使用HttpClient获取网页数据

 

    public class HttpHelper
    {
        static HttpClient client = new HttpClient();

        public static async Task<string> GetXmlAsync(string strRequestUrl)
        {
            string temp = "";
            try
            {
                temp = await client.GetStringAsync(strRequestUrl);
                return temp;
            }
            catch { return ""; }
        }


        public static void CancelHttpRequestAsync()
        {
            try
            {
                client.CancelPendingRequests();
            }
            catch { }
        }
    }

 

 

 在调用的时候,直接这样

 

var repsonse = await HttpHelper.GetXmlAsync(strRequestUrl);
            if (repsonse != null && repsonse != "")
{
         //TODO...........
}

 

 

你也可以做成想我的app那样的程序了。

posted @ 2018-03-19 16:22  猫叔Vincent  阅读(367)  评论(0编辑  收藏  举报