使用fiddle截獲post給伺服器的url和資料,使用httprequest類代碼post到伺服器
今天給出另外一種方法,就是利用winform控制項中的webbrowser控制項來操作頁面。
這裡我們會類比一個搜尋過程,開啟百度網站,輸入搜尋索引鍵,在搜尋結果中開啟串連
| 代碼如下 |
複製代碼 |
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 shdocvw.webbrowser).document as ihtmldocument2; doc.parentwindow.execscript("window.alert=null", "網頁特效"); doc.parentwindow.execscript("window.confirm=null", "javascript"); doc.parentwindow.execscript("window.open=null", "javascript"); doc.parentwindow.execscript("window.showmodaldialog=null", "javascript"); doc.parentwindow.execscript("window.close=null", "javascript"); } |
代碼寫到這裡已經提交搜尋了,webbrowser也會接收這次提交後的記過,下面我們需要在結果中開啟一個串連做實驗。
先查看一下搜尋結果的html原始碼
| 代碼如下 |
複製代碼 |
private void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { htmldocument doc = this.webbrowser1.document; if(doc.url.absoluteuri=="http://www.baidu.com/") { doc.getelementbyid("kw").innertext = "翁玉禮"; doc.getelementbyid("su").invokemember("click"); } if(doc.url.absoluteuri.contains("wd"))//根據url中的 { htmlelement table = doc.getelementbyid("1");//根據搜尋結果的html表,找到第一個搜尋結果的url,這裡類比 var html = table.children[0].children[0].innerhtml; var url = html.substring(html.indexof("href") + 6, html.indexof(" target")); this.webbrowser1.navigate(url); } } |