控制台版本,winform更简单
static string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/";
[STAThread]
static void Main( string[ ] args )
{
//注册事件
webBrowser.DocumentCompleted += WebBrowser_DocumentCompleted;
webBrowser.Navigate( "https://helloareyouok.lanzous.com/i94vape" );
Application.Run( );
}
//事件调用
private static void WebBrowser_DocumentCompleted( object sender , WebBrowserDocumentCompletedEventArgs e )
{
try
{
MessageBox.Show( "加载成功" );
WebBrowser webBrowser = (WebBrowser)sender;
var 文件名 = webBrowser.Document.Title.Replace(" - 蓝奏云", "");
// /html/body/div[2]/a
HtmlElement a = webBrowser.Document.Window.Frames[0].Document.GetElementsByTagName("div")[1].FirstChild;
//foreach( HtmlWindow item in webBrowser.Document.Window.Frames )
//{
// var go = item.Document.GetElementById("go");
// a = go.FirstChild ?? null;
//}
var href = a.GetAttribute("href");
MessageBox.Show( href );
httpWebRequest = HttpWebRequest.CreateHttp( href );
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
var uri=httpWebRequest.GetResponse().ResponseUri.ToString();
Console.WriteLine( 文件名 );
Console.WriteLine( uri );
if( File.Exists( desktopPath + 文件名 ) )
{
File.Delete( desktopPath + 文件名 );
}
new WebClient().DownloadFile( uri , desktopPath + 文件名 );
}
finally
{
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
}
}