本文告诉大家如何使用多个方式访问网页,可以获得网页源代码,可以做爬取网络信息。

Windows10 UWP 要访问 csdn博客,可以使用Windows.Web.Http.HttpClient,下面尝试访问一下我的博客 http://blog.csdn.net/lindexi_gd/article/details/50392343

我先在 xaml 添加一个 TextBlock ,这个 TextBlock 是 tb 用来拿到我访问页面拿到的内容


            string str = "http://blog.csdn.net/lindexi_gd/article/details/50392343";

            using (Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient())

            {

                try

                {

                    Windows.Web.Http.HttpResponseMessage response = await client.GetAsync(new Uri(str));

                    if (response != null && response.StatusCode == Windows.Web.Http.HttpStatusCode.Ok)

                    {

                        using (Windows.Storage.Streams.InMemoryRandomAccessStream stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())

                        {

                            await response.Content.WriteToStreamAsync(stream);

                            stream.Seek(0);                            

                            Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer((uint)stream.Size);

                            await stream.ReadAsync(buffer, (uint)stream.Size, Windows.Storage.Streams.InputStreamOptions.Partial);

                            using (Windows.Storage.Streams.DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(buffer))

                            {

                                tb.Text = dataReader.ReadString((uint)stream.Size);

                            }

                        }

                    }

                }

                catch

                {

                }

在前台有一个TextBlock,名字是 tb ,界面还有一个 按钮,按钮点击触发上面代码,访问博客,得到的内容放在 tb 显示

这时按下 F5 运行,可以看到下面的界面

除了 httpClient 还可以使用 HttpWebRequest ,请看下面


            System.Net.HttpWebRequest request = null;

            request = System.Net.WebRequest.Create(str) as System.Net.HttpWebRequest;

            request.Accept = "text/html, application/xhtml+xml, image/jxr, */*";
            //有些网站需要 Accept 如果这个不对,不返回

            request.Method = "GET";

            request.CookieContainer = new System.Net.CookieContainer();

            try

            {

                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)await request.GetResponseAsync();

                if (response != null && response.StatusCode==System.Net.HttpStatusCode.OK)

                {

                    tb.Text = response.ContentLength.ToString();

                    using (Stream stream= response.GetResponseStream())

                    {

                        byte[] buffer = new byte[10240];
                        //实际可以用其他方法

                        stream.Read(buffer, 0, 10240);

                        tb.Text = System.Text.Encoding.UTF8.GetString(buffer);
                        //在哪知道是UTF8?实际上解析网页这里比较难,我用的是知道他是 UTF8

                    }

                }

            }

            catch

            {

            }

需要注意 Windows.Web.Http.HttpClientSystem.Net.Http.HttpClient 是不相同,请看揭秘Windows10 UWP中的httpclient接口[2] - 蘑菇先生 - 博客园 和 void 大神写的 详解 UWP (通用 Windows 平台) 中的两种 HttpClient API

设置代理

现在的 UWP 程序只能使用 IE 的代理,而不能自定义代理,虽然存在 httpClientHandler.Proxy 可以设置 IWebProxy ,我也尝试写了自己的本地代理,但是没有访问

    public class WebProxy : IWebProxy
    {
        /// <inheritdoc />
        public Uri GetProxy(Uri destination)
        {
            return new Uri("socks5://127.0.0.1:10112");
        }

        /// <inheritdoc />
        public bool IsBypassed(Uri host)
        {
            return false;
        }

        /// <inheritdoc />
        public ICredentials Credentials { get; set; }
    }

我在 GetProxy 使用断点,在使用下面代码运行,没有进入刚才写的函数

            var httpClientHandler = new HttpClientHandler();
            httpClientHandler.UseProxy = true;
            httpClientHandler.Proxy = new WebProxy();

            var httpClient = new HttpClient(httpClientHandler);

            var str = await httpClient.GetStringAsync(new Uri("https://www.google.com"));

            Debug.WriteLine(str);

WebView

还有一个简单的方法是使用 WebView 就是 Edge 浏览器,所以通过浏览器可以做出更强大的效果。

先在界面添加一个按钮和控件

        <WebView x:Name="TraymorxasluPoocigur"></WebView>
        <Button HorizontalAlignment="Center" Content="确定" Click="FersamaltaiJearxaltray_OnClick"></Button>

在按钮点击的时候,尝试下面几个方式访问网页

        private void FersamaltaiJearxaltray_OnClick(object sender, RoutedEventArgs e)
        {
            TraymorxasluPoocigur.Navigate(new Uri("http://lindexi.github.io"));
        }

访问解决方案资源

        private void FersamaltaiJearxaltray_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                TraymorxasluPoocigur.Navigate(new Uri("ms-appx:///林德熙.html"));
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
            }
        }

参见:win10 uwp 访问解决方案文件

访问本地的文件

                var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///林德熙.html"));

                var folder = ApplicationData.Current.LocalFolder;

                var str = await FileIO.ReadTextAsync(file);

                file = await folder.CreateFileAsync("林德熙.html", CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteTextAsync(file, str);

                TraymorxasluPoocigur.Navigate(new Uri("ms-appdata:///local/林德熙.html"));

访问字符串

                var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///林德熙.html"));

                var str = await FileIO.ReadTextAsync(file);

                TraymorxasluPoocigur.NavigateToString(str);

参见: win10 uwp 模拟网页输入

如何使用 C# 爬虫获得专栏博客更新排行 - CSDN博客


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/win10-UWP-%E8%AE%BF%E9%97%AE%E7%BD%91%E9%A1%B5.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利