buffer = null;
System.Net.WebClient webClient = new System.Net.WebClient("https://music.douban.com/artists/genre_page/8/");
System.Net.WebClient webclient = webClient;
buffer = webclient.DownloadData("https://music.douban.com/artists/genre_page/8/");
string html = System.Text.Encoding.GetEncoding("utf-8").GetString(buffer);
uj5u.com熱心網友回復:
“WebClient”不包含采用 1 個引數的建構式 不是提示你了么 看看你引數個數 找你對應的這個方法 看看需要幾個引數uj5u.com熱心網友回復:
webclient只有無參建構式,你放引數進去干嘛?你百度搜一下.net webclient一抓一大把例子
uj5u.com熱心網友回復:
public class WebClientHelper
{
public static string DownloadString(string url)
{
WebClient wc = new WebClient();
//wc.BaseAddress = url; //設定根目錄
wc.Encoding = Encoding.UTF8; //設定按照何種編碼訪問,如果不加此行,獲取到的字串中文將是亂碼
string str = wc.DownloadString(url);
return str;
}
public static string DownloadStreamString(string url)
{
WebClient wc = new WebClient();
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
Stream objStream = wc.OpenRead(url);
StreamReader _read = new StreamReader(objStream, Encoding.UTF8); //新建一個讀取流,用指定的編碼讀取,此處是utf-8
string str = _read.ReadToEnd();
objStream.Close();
_read.Close();
return str;
}
public static void DownloadFile(string url, string filename)
{
WebClient wc = new WebClient();
wc.DownloadFile(url, filename); //下載檔案
}
public static void DownloadData(string url, string filename)
{
WebClient wc = new WebClient();
byte [] bytes = wc.DownloadData(url); //下載到位元組陣列
FileStream fs = new FileStream(filename, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
}
public static void DownloadFileAsync(string url, string filename)
{
WebClient wc = new WebClient();
wc.DownloadFileCompleted += DownCompletedEventHandler;
wc.DownloadFileAsync(new Uri(url), filename);
Console.WriteLine("下載中。。。");
}
private static void DownCompletedEventHandler(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine(sender.ToString()); //觸發事件的物件
Console.WriteLine(e.UserState);
Console.WriteLine(e.Cancelled);
Console.WriteLine("異步下載完成!");
}
public static void DownloadFileAsync2(string url, string filename)
{
WebClient wc = new WebClient();
wc.DownloadFileCompleted += (sender, e) =>
{
Console.WriteLine("下載完成!");
Console.WriteLine(sender.ToString());
Console.WriteLine(e.UserState);
Console.WriteLine(e.Cancelled);
};
wc.DownloadFileAsync(new Uri(url), filename);
Console.WriteLine("下載中。。。");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/68856.html
標籤:ASP
