zoukankan      html  css  js  c++  java
  • 2018-8-10-win10-UWP-访问网页

    title author date CreateTime categories
    win10 UWP 访问网页
    lindexi
    2018-08-10 19:16:51 +0800
    2018-2-13 17:23:3 +0800
    Win10 UWP

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

    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博客

  • 相关阅读:
    我的第一个Node.js项目
    map.keySet()获取map全部的key值
    sql语句的优化
    操作数据库的快捷键
    学习方法(准则)
    input输入框的readonly属性-----http://www.w3school.com.cn/tags/tag_input.asp
    Java反射机制(Reflect)解析-----https://www.cnblogs.com/fzz9/p/7738381.html
    java的动态代理机制详解-----https://www.cnblogs.com/xiaoluo501395377/p/3383130.html
    jQuery入门--- 非常好
    JQuery_九大选择器
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085505.html
Copyright © 2011-2022 走看看