WebClient doesn't exist on Windows Phone 8.1? Downloading html of site

别来无恙 提交于 2019-12-30 10:25:20

问题


I want to get source code of some website.

I found this solution:

var html = System.Net.WebClient().DownloadString(siteUrl);

But VisualStudio tells that WebClient does not exist in System.Net.

How to fix that? Or how to do it other way?

PS: does windows phone have some special tag which developers usually use when they looking for some code/solutions?


回答1:


WebClient does exist in WP8 like this:

WebClient thisclient = new WebClient();
thisclent.DownloadStringAsync(new Uri("urihere");
thisclient.DownloadStringCompleted += (s, x) =>
{
    if (x.Error != null)
    {
    //Catch any errors
    }
//Run Code
}

For 8.1 apps, use something like this:

    HttpClient http = new System.Net.Http.HttpClient();
    HttpResponseMessage response = await http.GetAsync("somesite");
    webresponse = await response.Content.ReadAsStringAsync();



回答2:


WebClient is available for Windows Phone Silverlight 8.1 apps. Windows Phone Runtime apps use Windows.Web.Http.HttpClient.

There is also a Portable HttpClient for .NET Framework and Windows Phone.




回答3:


This is what I currently use to download HTML source from webpages:

public static async Task<string> DownloadPageAsync(string pageURL)
    {
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await client.GetAsync(page))
        using (HttpContent content = response.Content)
        {
            string result = await content.ReadAsStringAsync();

            return result;
        }
    }

This function will return downloaded html of pageURL.



来源:https://stackoverflow.com/questions/26184968/webclient-doesnt-exist-on-windows-phone-8-1-downloading-html-of-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!