【问题标题】:C# Read StreamReader Webpage Uri RelativeC# 读取 StreamReader 网页 Uri 相关
【发布时间】:2011-07-12 09:46:29
【问题描述】:

我必须下载阅读此网页的内容,我正在使用此代码 它适用于 Windows 手机应用程序

string html = new StreamReader(Application.GetResourceStream(new Uri("http://www.knbsb.nl/nw/index.php?option=com_content&view=category&layout=blog&id=382&Itemid=150&lang=nl&LevelID=120&CompID=1580", UriKind.Relative)).Stream).ReadToEnd();

我知道 UriKind 设置为相对,但它必须用于其他脚本。

所以基本上我必须将网页从绝对 Uri 变成相对 Urikind。 但我不知道该怎么做!

【问题讨论】:

    标签: c# windows-phone-7 uri relative-path streamreader


    【解决方案1】:

    您需要异步发出请求。
    你可以使用这样的东西作为助手:

    public static void RequestAsync(Uri url, Action<string, Exception> callback)
    {
        if (callback == null)
        {
            throw new ArgumentNullException("callback");
        }
    
        try
        {
            var req = WebRequest.CreateHttp(url);
    
            AsyncCallback getTheResponse = ar =>
            {
                try
                {
                    string responseString;
    
                    var request = (HttpWebRequest)ar.AsyncState;
    
                    using (var resp = (HttpWebResponse)request.EndGetResponse(ar))
                    {
                        using (var streamResponse = resp.GetResponseStream())
                        {
                            using (var streamRead = new StreamReader(streamResponse))
                            {
                                responseString = streamRead.ReadToEnd();
                            }
                        }
                    }
    
                    callback(responseString, null);
                }
                catch (Exception ex)
                {
                    callback(null, ex);
                }
            };
    
            req.BeginGetResponse(getTheResponse, req);
        }
        catch (Exception ex)
        {
            callback(null, ex);
        }
    }
    

    然后您可以像这样拨打电话:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        RequestAsync(
            new Uri("http://www.knbsb.nl/nw/index.php?option=com_content&view=category&layout=blog&id=382&Itemid=150&lang=nl&LevelID=120&CompID=1580"),
            (html, exc) =>
                {
                    if (exc == null)
                    {
                        Dispatcher.BeginInvoke(() => MessageBox.Show(html));
                    }
                    else
                    {
                        // handle exception appropriately
                    }
                });
    }
    

    【讨论】:

    • 这似乎可行,因为下载字符串不适用于手机。但我想将 htlm 字符串添加到这个 ://////// m_doc = new HtmlDocument(); //////////// m_doc.LoadHtml(html);//////// 但我做不到?
    • @Lark125 你从哪里得到一个 HtmlDocument 类?手机不包含一个作为平台 SDK 的一部分。你最终想要达到什么目标?你可能还想看看htmlagilitypack.codeplex.com
    • 我是从 htmlagility 包中获取的,但他们无法将绝对路径加载为网络地址
    • @Lark125 那么如果你尝试拨打.LoadHtml(html) 我在上面拨打MessageBox.Show 会发生什么?
    • 所以你的意思是 Dispatcher.BeginInvoke(()=> m_doc.LoadHtml(html) ?
    【解决方案2】:

    您可以使用WebClient 来做到这一点。

      using (var client = new WebClient())
            {
                string result = client.DownloadString("http://www.youtsite.com");
    //do whatever you want with the string. 
    
        }
    

    【讨论】:

    • WebClient 在手机上不是最理想的,因为它主要在 UIThread 上运行。
    • DownloadString 在手机上不可用。只有异步版本
    【解决方案3】:

    Application.GetResourceStream 用于从应用程序包中读取资源,而不是用于从 Web 请求资源。

    改用HttpWebRequestWebClient 类。

    例子:

    string html;
    using (WebClient client = new WebClient()) {
      html = client.DownloadString("http://www.knbsb.nl/nw/index.php?option=com_content&view=category&layout=blog&id=382&Itemid=150&lang=nl&LevelID=120&CompID=1580");
    }
    

    【讨论】:

    • WebClient 的同步方法在 WP7 上不可用。此外,在 7.0 中建议不要使用 WebClient,因为它总是在 UI 线程上返回。
    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    相关资源
    最近更新 更多