procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant); var o:OleVariant; begin o := WebBrowser1.OleObject.document.all.item('UserName',0); o.value:=edit1.text; o := WebBrowser1.OleObject.document.all.item('Password',0); o.value:=edit2.text; o := WebBrowser1.OleObject.document.all.item('Logon',0); o.click; end; 你要查看源文件知道输入用户名,密码和登陆按钮的名称
var
doc:ihtmldocument2;
input:olevariant;
user,pwd,pp:ihtmlinputelement;
begin
try
doc := webbrowser1.document as ihtmldocument2;
user := (doc.all.item('username',0) as ihtmlinputelement);
user.value := '';//取值给网页的用户名文本框
pwd := (doc.all.item('password',0) as ihtmlinputelement);
pwd.value := '';//取值给网页的密码文本框
input := doc.all.item('submit',0);
input.click;
except end
end;
不过这样感觉速度慢。
TIdHttp.Request.Username
TIdHttp.Request.Password
TIdHttp.Request.BasicAuthentication
TIdHttp.OnSelectAuthorization
TIdHttp.OnAuthorization如果网站采用Basic认证(最常见的情况),那么直接把TIdHttp.Request.BasicAuthentication改为True,并在TIdHttp.Post/TIdHttp.Get之前设置TIdHttp.Request.Username和TIdHttp.Request.Password即可。如果网站采用其他方式认证,则必须在TIdHttp.SelectAuthorization事件中指定对应的认证对象的类型,并在OnAuthorization事件中提供用户名和密码。
<form method="POST" action="login.asp">
<input type="edit" name="username">
<input type="edit" name="password">
……
</form>如果method是POST,那么你就需要用TIdHttp.Post方法,如果是GET,就用TIdHttp.Get方法。提交时浏览器生成的目标网址为:http://xxx.xxx.xxx/login.asp?username=xxx&password=xxx你只需要用程序组合出正确的URL,用正确的TIdHttp方法,就可以实现自动登录了。当然,需要检查TIdHttp.Get和TIdHttp.Post方法执行后TIdHttp.Response对象,因为很多网站是利用Cookie来保持登录状态的。可以遍历TIdHttp.Response.RawHeaders,看看有没有一行是"Set-Cookie: xxxxx",如果有,就把xxxx保存下来,以后每次提交其他内容时,加上以下几行:TIdHttp.Request.CustomHeaders.Clear;
TIdHttp.Request.CustomHeaders.Append("Cookie: " + "xxxxxx");还有不明白的随时提问哦。我最近正好也在做这些东西。
const pDisp: IDispatch; var URL: OleVariant);
var
o:OleVariant;
begin
o := WebBrowser1.OleObject.document.all.item('UserName',0);
o.value:=edit1.text;
o := WebBrowser1.OleObject.document.all.item('Password',0);
o.value:=edit2.text;
o := WebBrowser1.OleObject.document.all.item('Logon',0);
o.click;
end;
你要查看源文件知道输入用户名,密码和登陆按钮的名称
WebBrowser1.Navigate('http://www.baidu.com');
查看网站的源码,找到用户名和密码的input,用webbrowser中的方法给input赋值,然后模拟点击登录按钮
不过淘宝的登录用户名和密码框不是简单的网页元素input,貌似是ocx实现的,不知道用webbrowser怎么实现这样的