2010年7月30日 星期五

[C#] 自動登入/控制網頁



string IE_ID = "guest"; //帳號部分
string IE_PW = "qkqk"; //密碼
mshtml.IHTMLDocument2 IE__DOC; //網頁內容
mshtml.HTMLInputElementClass IE_INPUT; //輸入 Tag
SHDocVw.InternetExplorer IE_AUTO_OPEN_IE = null; //IE 視窗
object missing = null; //開起IE navigate用

if (IE_AUTO_OPEN_IE == null)
{
IE_AUTO_OPEN_IE = new SHDocVw.InternetExplorer();
IE_AUTO_OPEN_IE.Visible = true;
IE_AUTO_OPEN_IE.Navigate("http://www.kimo.com.tw", ref missing, ref missing, ref missing, ref missing);

while (IE_AUTO_OPEN_IE.StatusText.IndexOf("完成") == -1)
{
Application.DoEvents();
}

//開始處理
IE__DOC = (mshtml.IHTMLDocument2)IE_AUTO_OPEN_IE.Document;
foreach (mshtml.IHTMLElement MyElement in IE__DOC.all)
{
if (MyElement.tagName == "INPUT")
{
//轉型
IE_INPUT = ((mshtml.HTMLInputElementClass)MyElement);

//帳號
if (IE_INPUT.name == "UserID")
{
IE_INPUT.value = IE_ID;
}

//密碼
if (IE_INPUT.name == "UserPass")
{
IE_INPUT.value = IE_PW;
}

//登入
if (IE_INPUT.value == "Login")
{
IE_INPUT.click();
}
}
}

while (true)
{
if (IE_AUTO_OPEN_IE.Busy == false && IE_AUTO_OPEN_IE.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
break;
}
System.Threading.Thread.Sleep(500);
}

if (true)
{
Application.DoEvents();
IE__DOC = (mshtml.IHTMLDocument2)IE_AUTO_OPEN_IE.Document;

foreach (mshtml.IHTMLElement MyElement in IE__DOC.all)
{
if (MyElement.tagName == "INPUT")
{
//轉型
IE_INPUT = ((mshtml.HTMLInputElementClass)MyElement);
if (IE_INPUT.name == "UnitNo")
{
IE_INPUT.value = textBox3.Text.Trim();
}
}
}



轉貼http://kuomingwang.blogspot.com/2010/07/c.html

如何自動找到正確的IE視窗並自動登入Gmail or Yahoo Mail

Microsfot 及 其他一些廠商的指紋機有提供一個 網頁自動登入的功能
http://www.fineart.com.tw/fineart_web/products/fp/fp_wl.htm

這是如何辦到的呢? 下面範例示範如何自動找到正確的IE視窗並自動登入Gmail or Yahoo Mail

使用這個範例必需在參考中加入 (在COM的那一個頁籤中)
Microsoft HTML Object Library 及 Microsoft Internet Controls


1private void AutoSignInGmail()
2{
3 string ID = "YourID";
4 string PWD = "YourPassword";
5
6 mshtml.IHTMLDocument2 MyDoc; //網頁內文
7 mshtml.HTMLInputElementClass MyInput; //Input Tag
8 SHDocVw.InternetExplorer AutoIE = null; //IE視窗
9 object missing = null; //開起IE navigate用
10
11 #region 找看看有沒有現在的IE是在登入頁
12 //取得目前 Shell 的所有 Windows
13 SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();
14 //對所有的視窗
15 foreach (SHDocVw.InternetExplorer MyIE in shellWindows)
16 {
17 //如果是IE
18 if (MyIE.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
19 {
20 MyDoc = (mshtml.IHTMLDocument2)MyIE.Document;
21 //我們以Title判斷登入頁是否存在
22 if (MyDoc.title == "歡迎使用 Gmail") //Gmail
23 //if (MyDoc.title == "Yahoo!奇摩電子信箱") //YahooMail
24 {
25 AutoIE = MyIE; //如果有找到
26 break;
27 }

28 }

29 }

30 shellWindows = null;
31 #endregion

32
33 #region 如果沒找到開一個新IE
34 //如果沒找到
35 if (AutoIE == null)
36 {
37 //建一個新IE
38 AutoIE = new SHDocVw.InternetExplorer();
39 AutoIE.Visible = true;
40 AutoIE.Navigate("http://gmail.com/",
41 ref missing, ref missing, ref missing, ref missing); //Gmail
42 //AutoIE.Navigate("http://mail.kimo.com.tw/",
43 // ref missing, ref missing, ref missing, ref missing); //YahooMail
44
45 //等待頁面載入完成, 也可以用DocumentComplete Event
46 while (AutoIE.StatusText.IndexOf("完成")==-1)
47 Application.DoEvents();
48 }

49 #endregion

50
51 #region 搜尋IE網頁內文找到正確欄位並填入值
52 //取得IE網頁內文
53 MyDoc = (mshtml.IHTMLDocument2)AutoIE.Document ;
54 //對每一個Tag
55 foreach (mshtml.IHTMLElement MyElement in MyDoc.all)
56 {
57 //如果是 Input Tag
58 if (MyElement.tagName == "INPUT")
59 {
60 //轉型
61 MyInput = ((mshtml.HTMLInputElementClass)MyElement);
62
63 //是不是帳號欄位
64 if (MyInput.name=="Email") //Gmail
65 //if (MyInput.name=="login") //YahooMail
66 MyInput.value = ID;
67
68 //是不是密碼欄位
69 if (MyInput.name=="Passwd") //Gmail
70 //if (MyInput.name=="passwd") //YahooMail
71 MyInput.value = PWD;
72
73 //是不是登入的按鈕
74 if (MyInput.name=="null") //Gmail
75 //if (MyInput.name=="submit") //YahooMail
76 MyInput.click();
77 }

78 }

79 #endregion

80
81 AutoIE = null;
82}

83

轉至http://blog.blueshop.com.tw/timothychi/archive/2006/03/09/19083.aspx