开发者

C# Read StreamReader Webpage Uri Relative

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=ca开发者_如何学JAVAtegory&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!


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


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. 

    }


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜