判断是否网络正常
private bool IsConnectedToInternet()
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int Desc;
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return internet.InternetGetConnectedState(out Desc, 0);
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](/syntaxhighlighting/OutliningIndicators/None.gif)
设置默认页面:
![](/syntaxhighlighting/OutliningIndicators/None.gif)
webBrowser1.Navigate(new Uri("http://www.ic2ic.com/copy.jsp"));
![](/syntaxhighlighting/OutliningIndicators/None.gif)
登录代码:
![](/syntaxhighlighting/OutliningIndicators/None.gif)
private void btnlogin_Click(object sender, EventArgs e)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
webBrowser1.Document.GetElementById("id").InnerText =txtuser.Text.ToString().Trim();//fill name
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
webBrowser1.Document.GetElementById("passwd").InnerText =txtpwd.Text.ToString().Trim();//fill pwd
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HtmlElement formLogin = webBrowser1.Document.Forms["frmLogin"];
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
formLogin.InvokeMember("submit");
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Thread.Sleep(5000);
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
herfclick("http://www.ic2ic.com/upload.jsp");//这是登录成功的操作,跳转到//upload.asp
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//注意不是直接跳过去的,模拟点击链接
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//SESSION不会丢失
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
herfclick("http://www.ic2ic.com/copy.jsp");
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](/syntaxhighlighting/OutliningIndicators/None.gif)
模拟点击链接:
![](/syntaxhighlighting/OutliningIndicators/None.gif)
private void herfclick(string url)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < webBrowser1.Document.All.Count; i++)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (webBrowser1.Document.All[i].TagName == "A" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim() ==url)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
webBrowser1.Document.All[i].InvokeMember("click");//引发”CLICK”事件
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](/syntaxhighlighting/OutliningIndicators/None.gif)
运行网页中的JS函数
![](/syntaxhighlighting/OutliningIndicators/None.gif)
private void callJsMethod(string jsfun,string jsParameter)//函数名字,函数参数
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HtmlDocument doc = webBrowser1.Document;
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
doc.InvokeScript(jsfun,new object[]...{jsParameter});
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](/syntaxhighlighting/OutliningIndicators/None.gif)
填充数据:
![](/syntaxhighlighting/OutliningIndicators/None.gif)
private void filldate(string txt)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HtmlElement frmWord = webBrowser1.Document.Forms["frmWord"];//找到from
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HtmlElementCollection txtarea = frmWord.GetElementsByTagName("TEXTAREA");
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HtmlElementCollection radio = frmWord.GetElementsByTagName("INPUT");
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
radio[2].InvokeMember("click");//选择radio控件
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
txtarea[0].InnerText = txt.ToString();
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
转载于:https://blog.51cto.com/657956565/198865