原詢問者嘗試用 TIdHTTP + TIdSSLIOHandlerSocketOpenSSL 模擬 POST 登入博客園,涉及 __VIEWSTATE、__EVENTVALIDATION 等動態隱藏欄位,極易失效。
TWebBrowser 到表單。WebBrowser1.Navigate('https://passport.cnblogs.com/login.aspx');DocumentComplete 事件中注入腳本或直接填寫表單:procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
var
Doc: IHTMLDocument2;
InputUser, InputPwd, BtnLogin: IHTMLElement;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
InputUser := Doc.getElementById('input1') as IHTMLElement;
InputPwd := Doc.getElementById('input2') as IHTMLElement;
BtnLogin := Doc.getElementById('btnLogin') as IHTMLElement;
if Assigned(InputUser) and Assigned(InputPwd) and Assigned(BtnLogin) then
begin
(InputUser as IHTMLInputElement).value := '你的用戶名';
(InputPwd as IHTMLInputElement).value := '你的密碼';
(BtnLogin as IHTMLInputElement).click;
end;
end;
__VIEWSTATE 等隱藏欄位。Delphi 11+ 支援 TEdgeBrowser,效能更好、相容性更強,建議新專案採用。
模擬登入複雜站點,**瀏覽器元件**遠比手工 HTTP 請求可靠。