C# 读取 StreamReader 网页 Uri 相对值

发布于 2024-11-19 20:55:22 字数 440 浏览 2 评论 0原文

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

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。 但我不知道该怎么做!

I got to Download the read the content of this webpage i am using this code
it is for a windows phone app

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();

I know that the UriKind is set Relative, but it has to be for the other script.

So basically i have to make the webpage to a relative Urikind from a absolute Uri.
But I don't know how to do that!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天暗了我发光 2024-11-26 20:55:23

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

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
                }
            });
}

You need to make the request asynchronously.
You can use something like this as a helper:

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);
    }
}

You can then make calls like this:

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
                }
            });
}
删除→记忆 2024-11-26 20:55:23

您可以使用 WebClient 来执行此操作。

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

    }

You can make use of WebClient to do it.

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

    }
高跟鞋的旋律 2024-11-26 20:55:23

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");
}

The Application.GetResourceStream is for reading resources from the application package, not for requesting resources from the web.

Use the HttpWebRequest or WebClient classes instead.

Example:

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文