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/");
A string of HTML=System. Text. Encoding. GetEncoding (" utf-8 "). Get string (buffer);
CodePudding user response:
"WebClient" does not include using a parameter of the constructor is not prompt you? Look at the number of parameters you find you corresponding to this method requires several parametersCodePudding user response:
Webclient only a no-parameter constructor, you put parameters? You baidu search.net webclient one to grasp a large exampleCodePudding user response:
public class WebClientHelper
{
Public static string DownloadString (string url)
{
WebClient wc=new WebClient ();
//wc. BaseAddress=url;//set the root directory
Wc. Encoding=Encoding. UTF8;//set according to what the code to access, without coming here, get to the Chinese string will be garbled
String STR=wc. DownloadString (url);
return str;
}
Public static string DownloadStreamString (string url)
{
WebClient wc=new WebClient ();
Wc. Headers. Add (" the 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);//to create a new read flow, using the specified encoding read, this is a utf-8
String STR=_read. ReadToEnd ();
ObjStream. Close ();
_read. Close ();
return str;
}
Public static void DownloadFile (url string, the string filename)
{
WebClient wc=new WebClient ();
Wc. DownloadFile (url, filename);//download file
}
Public static void DownloadData (url string, the string filename)
{
WebClient wc=new WebClient ();
Byte [] bytes=wc. DownloadData (url);//download to byte array
FileStream fs=new FileStream (filename, FileMode. Create);
Fs. Write (bytes, 0, bytes. Length);
Fs. Flush ();
Fs. The Close ();
}
Public static void DownloadFileAsync (url string, the string filename)
{
WebClient wc=new WebClient ();
Wc. DownloadFileCompleted +=DownCompletedEventHandler;
Wc. DownloadFileAsync (new Uri (url), filename);
Console. WriteLine (" download,,, ");
}
Private static void DownCompletedEventHandler (object sender, AsyncCompletedEventArgs e)
{
Console. WriteLine (sender. ToString ());//the triggering event object
Console. WriteLine (e.U serState);
Console. WriteLine (e.c. with our fabrication: ancelled);
Console. WriteLine (" asynchronous download is complete!" );
}
Public static void DownloadFileAsync2 (url string, the string filename)
{
WebClient wc=new WebClient ();
Wc. DownloadFileCompleted +=(sender, e)=& gt;
{
Console. WriteLine (" the download is complete!" );
Console. WriteLine (sender. ToString ());
Console. WriteLine (e.U serState);
Console. WriteLine (e.c. with our fabrication: ancelled);
};
Wc. DownloadFileAsync (new Uri (url), filename);
Console. WriteLine (" download,,, ");
}
}