Asp tutorial. net webbrowser webpage code
Use fiddle to intercept the url and data sent to the server, and use the httprequest class code to post to the server.
Another method provided today is to use the webbrowser control in the winform control to operate the page.
Here we will simulate a search process, open the Baidu website, enter the search keyword, and open the connection in the search results
Public form1 ()
{
Initializecomponent ();
(Webbrowser1.activexinstance as shdocvw. webbrowser). navigatecomplete2 + =
New shdocvw. dwebbrowserevents2_navigatecomplete2eventhandler (form1_navigatecomplete2 );
}
Void form1_navigatecomplete2 (object pdisp, ref object url)
{
Ihtmldocument2 doc = (webbrowser1.activexinstance as shdocv?webbrowser=.doc ument as ihtmldocument2;
Doc.parentwindow.exe cscript ("window. alert = null", "webpage effect ");
Doc.parentwindow.exe cscript ("window. confirm = null", "javascript ");
Doc.parentwindow.exe cscript ("window. open = null", "javascript ");
Doc.parentwindow.exe cscript ("window. showmodaldialog = null", "javascript ");
Doc.parentwindow.exe cscript ("window. close = null", "javascript ");
}
The code is written here and submitted for search. webbrowser will also receive the record after this submission. Next we need to open a connection in the result to do the experiment.
First, check the html source code of the search result.
Private void webbrowserappsdocumentcompleted (object sender, webbrowserdocumentcompletedeventargs e)
{
Htmldocument doc = this.webbrowser1.doc ument;
If (doc. url. absoluteuri = "http://www.baidu.com /")
{
Doc. getelementbyid ("kw"). innertext = "Weng Yuli ";
Doc. getelementbyid ("su"). invokemember ("click ");
}
If (doc. url. absoluteuri. contains ("wd ") //
{
Htmlelement table = doc. getelementbyid ("1"); // locate the url of the first search result based on the html table of the search result.
Var html = table. children [0]. children [0]. innerhtml;
Var url = html. substring (html. indexof ("href") + 6, html. indexof ("target "));
This. webbrowser1.navigate (url );
}
}