public partial class Form1 : Form
{
bool timeout = false;
public Form1()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
textBox1.Text = "";
textBox2.Text = "";
web_con("https://nportal.ntut.edu.tw");
progressBar1.Maximum = 100;//設置最大長度值
progressBar1.Value = 0;//設置當前值
progressBar1.Step = 20;//設置沒次增長多少
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;//設置當前值
if ((textBox1.Text != string.Empty)&& (textBox2.Text != string.Empty)&& (textBox3.Text != string.Empty) && !(graduated.Checked&&ungraduated.Checked))
{
tabControl1.SelectedTab = tabPage2;
progressBar1.Value += progressBar1.Step;//讓進度條增加一次
logword_pass();
string ss;
if (ungraduated.Checked == true){ ss = "教務系統"; }
else { ss = " 畢業生系統 "; }
bool a = Check_Complete(ss, 15000);
if (a)
{ //----------------------------------------------------------------------------
progressBar1.Value += progressBar1.Step;//讓進度條增加一次
webBrowser1.Document.GetElementById("apFolder").InvokeMember("click");
bool b = Check_Complete("學業成績查詢專區", 3000);
if (b)
{
progressBar1.Value += progressBar1.Step;//讓進度條增加一次
if (ungraduated.Checked == true)
{
HtmlElement aaa = webBrowser1.Document.GetElementById("ap-aa").GetElementsByTagName("span")[7].GetElementsByTagName("a")[0];//apFolder
aaa.OuterHtml = aaa.OuterHtml.Replace("blank", "self");
HtmlElement bbb = webBrowser1.Document.GetElementById("ap-aa").GetElementsByTagName("span")[7].GetElementsByTagName("a")[0];
bbb.InvokeMember("click");
}
else if(graduated.Checked == true)
{
HtmlElement aaa = webBrowser1.Document.GetElementById("ap-Graduate").GetElementsByTagName("span")[1].GetElementsByTagName("a")[0];
aaa.OuterHtml = aaa.OuterHtml.Replace("blank", "self");
HtmlElement bbb = webBrowser1.Document.GetElementById("ap-Graduate").GetElementsByTagName("span")[1].GetElementsByTagName("a")[0];
bbb.InvokeMember("click");
}
else
{
error_message("impossble");
}
//------------------------------------------------------------------------------
bool c = Check_Complete("學生查詢專區", 3000);
if (c)
{
progressBar1.Value += progressBar1.Step;//讓進度條增加一次
webBrowser1.Navigate("http://aps-stu.ntut.edu.tw/StuQuery/QryScore.jsp");
bool d = Check_Complete("操行成績", 3000);
if (d)
{
progressBar1.Value += progressBar1.Step;//讓進度條增加一次
webBrowser1.Document.GetElementById("QryAll").InvokeMember("click");
MessageBox.Show("OK");
}
else
{
MessageBox.Show("成績頁面無法開啟,請提報錯誤給網站管理人");
web_con("https://nportal.ntut.edu.tw");
}
}
else
{
MessageBox.Show("學生查詢專區無法開啟,請提報錯誤給網站管理人");
web_con("https://nportal.ntut.edu.tw");
}
}
else
{
MessageBox.Show("學業成績查詢專區無法開啟,請提報錯誤給網站管理人");
web_con("https://nportal.ntut.edu.tw");
}
}
else
{
MessageBox.Show("帳號錯誤或是學校伺服器回應過慢導致逾時跳出");
web_con("https://nportal.ntut.edu.tw");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string gethtmlcode;
gethtmlcode = gethtml();
int[] a = get_all_indexof_from_artical(gethtmlcode, @"<table");
string[] b = new string[a.Length];
string[][] c = new string[a.Length][];
string[][] d = new string[a.Length][];
for (int i = 0; i < a.Length; i++)
{
b[i] = capturestring(gethtmlcode, @"<table", @"/table>", a[i]);
c[i] = get_data_form_htmltag(b[i]);
for (int j = 0; j < get_data_form_htmltag(b[i]).Length; j++)
{
c[i][j] = remove_htmltag(c[i][j]);
}
d[i] = suit_Array_with_no_block(c[i]);
}
string f = "";
for (int i = 0; i < a.Length; i++)
{
f += Environment.NewLine;
for (int j = 8; j < d[i].Length - 9; j++)
{
if ((j % 8 == 0) && (j != 0))
{
f += Environment.NewLine;
f += d[i][j] + " ";
}
else
{
f += d[i][j] + " ";
}
}
}
textBox4.Text = f;
}
private void loading()
{
while (!(webBrowser1.ReadyState == WebBrowserReadyState.Complete))
{
Application.DoEvents();
}
}
private bool Check_Complete(string keyword ,int timeout_time)
{
bool getkeyword = false;
System.Timers.Timer t = new System.Timers.Timer(timeout_time); // 等多久
t.Elapsed += new System.Timers.ElapsedEventHandler(check_timeout); //呼叫副程式
t.AutoReset = false; //设置是执行一次(false)还是一直执行(true);
t.Enabled = true;
while (!getkeyword)
{
if(timeout)
{
t.Close();
timeout = false;
return false;
}
foreach (HtmlElement he in webBrowser1.Document.All)
{
if (he.OuterText == keyword)
{
timeout = false;
t.Close();
getkeyword = true;
}
}
Application.DoEvents();
}
return true;
}
private void logword_pass()
{
webBrowser1.Document.GetElementById("muid").SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementById("mpassword").SetAttribute("value", textBox2.Text);
webBrowser1.Document.GetElementById("authcode").SetAttribute("value", textBox3.Text);
webBrowser1.Document.GetElementById("Submit2").InvokeMember("click");
}
private string gethtml()
{
string gethtmlcode;
System.IO.StreamReader getReader = new System.IO.StreamReader(this.webBrowser1.DocumentStream, System.Text.Encoding.Default);
gethtmlcode = getReader.ReadToEnd();
return gethtmlcode;
}
private string[] get_data_form_htmltag(string strTest)//get data between ">" and "<"
{
string[] matchvalue = new string[1000];
int i = 0;
// <th??>.....<th??> -> >...<th
//>....</a>
// @"<[^>]*>"
//[a-zA-Z0-9_]
//>[^<]*<
MatchCollection matches = Regex.Matches(strTest, @">[^<]*<");
foreach (Match match in matches)
{
matchvalue[i] = match.Value.ToString();
i++;
}
if (i < 0)
{
error_message("no one word can remove");
return null;
}
else
{
string[] get_value_Array = new string[i];
Array.Copy(matchvalue, get_value_Array, i);
return get_value_Array;
}
}
private string capturestring(string text, string initial_text, string final_text) //"capturestring" means that you want to capture from "text" between "initial_text" and "final_text".it will return "carturedtext"
{
string capturedtext = "";
int intial, final;
intial = text.IndexOf(initial_text);
if (intial == -1)//check initial_text is existed
{
error_message("can not find intial_text");
}
else
{
final = text.IndexOf(final_text);
if (final == -1)//check final_text is existed
{
error_message("can not find final_text");
}
else
{
text = text.Remove(final);
text = text.Remove(0, intial);
text = remove_htmltag_2(text);
capturedtext = text;
}
}
return capturedtext;
}
private string capturestring(string text, string initial_text, string final_text, int startIndex) //"capturestring" means that you want to capture from "text" between "initial_text" and "final_text". it will return "carturedtext" if your "capturestring" is more one, you can change initial_start_location to get more
{
string capturedtext = "";
int intial, final;
intial = text.IndexOf(initial_text, startIndex);
if (intial == -1)//check initial_text is existed
{
error_message("can not find intial_text");
}
else
{
final = text.IndexOf(final_text, startIndex);
if (final == -1)//check final_text is existed
{
error_message("can not find final_text");
}
else
{
text = text.Remove(final);
text = text.Remove(0, intial);
text = remove_htmltag_2(text);
capturedtext = text;
}
}
return capturedtext;
}
private int[] get_all_indexof_from_artical(string text, string keyword)//get all keyword indexof
{
int[] allindexofnum = new int[100];
int num, i = 0;
bool more = false;
num = text.IndexOf(keyword);
if (num < 0)
{
error_message("can not find any keyword");
}
else
{
more = true;
allindexofnum[i] = num;
i++;
while (more)
{
num = text.IndexOf(keyword, num + 1);
if (num < 0)
{
// MessageBox.Show("can not find more keyword");
more = false;
}
else
{
allindexofnum[i] = num;
i++;
}
}
}
int[] indexofnum = new int[i];
for (int j = 0; j < i; j++)
{
indexofnum[j] = allindexofnum[j];
}
return indexofnum;
}
private string remove_htmltag(string htmltext)//you can add another tag that you want to remove
{
htmltext = htmltext.Replace("<", "");
htmltext = htmltext.Replace(">", "");
htmltext = htmltext.Replace("\n", "");
htmltext = htmltext.Replace("\r", "");
htmltext = htmltext.Replace(" ", "");
return htmltext;
}
private string remove_htmltag_2(string htmltext)//you can add another tag that you want to remove
{
htmltext = htmltext.Replace("<B>", "");
htmltext = htmltext.Replace("</B>", "");
htmltext = htmltext.Replace("<BR>", "");
return htmltext;
}
private string[] suit_Array_with_no_block(string[] Array_with_block)//remove ""(<-yes this one, no data block) from Array
{
string[] a = new string[500];
int i = 0;
for (int j = 0; j < Array_with_block.Length; j++)
{
if ((Array_with_block[j] != ""))
{
a[i] = Array_with_block[j];
i++;
}
}
string[] sutableArray = new string[i];
Array.Copy(a, sutableArray, i);
return sutableArray;
}
private void error_message(string error_message)
{
if (error_message == "can not find any keyword") { MessageBox.Show("can not find any keyword"); }
else if (error_message == "can not find intial_text") { MessageBox.Show("can not find intial_text"); }
else if (error_message == "can not find final_text") { MessageBox.Show("can not find final_text"); }
else if (error_message == " no one word can remove") { MessageBox.Show(" no one word can remove"); }
else if (error_message == " impossble") { MessageBox.Show(" impossble"); }
else { };
}
public void check_timeout(object source, System.Timers.ElapsedEventArgs e)
{
timeout = true;
}
private void web_con(string url)
{
webBrowser1.Navigate(url);
loading();
HtmlDocument document = webBrowser1.Document;//获取控件中的html文档,类似于网页中的document对象。
document.Window.ScrollTo(610, 222);//将文档窗体滚动到指定位置,610表示横坐标
}
private void button3_Click(object sender, EventArgs e)
{
web_con("https://nportal.ntut.edu.tw");
}
}