參考自Chatgpt
public bool Checkin(string username, string password)
{
var autoLoginService = new AutoLoginService();
bool defaultPageContent = false;
//defaultPageContent = autoLoginService.Login(username, password, strLoginpage, strquerypage);
return defaultPageContent;
}
public bool Login(string username, string password, string strlogin, string strquery)
{
bool bolr = false;
//SSL TLS1.2
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate
{
return CheckCert();
};
//for SSL
System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
using (HttpClientHandler handler = new HttpClientHandler { UseCookies = true })
using (HttpClient client = new HttpClient(handler))
{
// 1️⃣ 先發送 GET 請求取得 __VIEWSTATE 和 __EVENTVALIDATION
HttpResponseMessage getResponse = client.GetAsync(strlogin).Result;
string loginPageHtml = getResponse.Content.ReadAsStringAsync().Result;
//Writelog(loginPageHtml);
// 解析 __VIEWSTATE 和 __EVENTVALIDATION
string viewState = Regex.Match(loginPageHtml, "id=\"__VIEWSTATE\" value=\"(.*?)\"").Groups[1].Value;
string eventValidation = Regex.Match(loginPageHtml, "id=\"__EVENTVALIDATION\" value=\"(.*?)\"").Groups[1].Value;
Writelog($"VIEWSTATE: {viewState}");
Writelog($"EVENTVALIDATION: {eventValidation}");
Writelog("txtUser = " + username);
Writelog("txtPwd = " + password);
// 2️⃣ 準備登入請求的 Form Data
var loginData = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("__VIEWSTATE", viewState),
new KeyValuePair<string, string>("__EVENTVALIDATION", eventValidation),
new KeyValuePair<string, string>("txtOrgCode", "BOTOSB"),
new KeyValuePair<string, string>("txtUser", username), // 請更換成你的帳號輸入框名稱
new KeyValuePair<string, string>("txtPwd", password), // 請更換成你的密碼輸入框名稱
new KeyValuePair<string, string>("cmdLogin", "Login") // 請更換成你的登入按鈕名稱
});
// 3️⃣ 發送 POST 登入請求
HttpResponseMessage postResponse = client.PostAsync(strlogin, loginData).Result;
// 4️⃣ 檢查是否成功登入
if (postResponse.IsSuccessStatusCode)
{
Writelog(postResponse.Content.ReadAsStringAsync().Result.Substring(0, 1500));
//Console.WriteLine("登入成功!");
bolr = true;
// 5️⃣ 使用登入後的 Cookie 訪問 Default 頁面
//HttpResponseMessage defaultPageResponse = client.GetAsync(strquery).Result;
//string defaultPageHtml = defaultPageResponse.Content.ReadAsStringAsync().Result;
//Writelog(defaultPageHtml); // 只顯示部分 HTML
}
else
{
//Console.WriteLine("❌ 登入失敗!");
bolr = false;
}
}
return bolr;
}