我想获得一些网站的源代码。
我发现这个解决scheme:
var html = System.Net.WebClient().DownloadString(siteUrl);
但VisualStudio告诉WebClient在System.Net中不存在。
如何解决这个问题? 或者如何做到这一点呢?
PS:windows phone有一些特殊的标签,开发人员在寻找代码/解决scheme时通常会使用这些标签吗?
WebClient确实存在于WP8中,如下所示:
WebClient thisclient = new WebClient(); thisclent.DownloadStringAsync(new Uri("urihere"); thisclient.DownloadStringCompleted += (s, x) => { if (x.Error != null) { //Catch any errors } //Run Code }
对于8.1应用程序,使用这样的东西:
HttpClient http = new System.Net.Http.HttpClient(); HttpResponseMessage response = await http.GetAsync("somesite"); webresponse = await response.Content.ReadAsStringAsync();
WebClient适用于Windows Phone Silverlight 8.1应用程序。 Windows Phone运行时应用程序使用Windows.Web.Http.HttpClient 。
还有一个用于.NET Framework和Windows Phone的可移植HttpClient 。
这是我目前用来从网页下载HTML源代码的东西:
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; } }
这个函数会返回pageURL的下载html。