用TWebBrowser组件在DELPHI中POST数据和取得网页源



unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;

type
TForm1 = class(TForm)
     WebBrowser1: TWebBrowser;
     Memo1: TMemo;
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
private
     { Private declarations }
public
     { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
URL:OleVariant;
POST:OleVariant;
target:OleVariant;
flag:OleVariant;
header:OleVariant;
PostStr:String;
PostLen,Index:Integer;
begin
URL:='http://port.fudan.edu.cn:58080/amserver/UI/Login?goto=http%3A%2F%2Fwww.urp.fudan.edu.cn%3A81%2Fepstar%2Frs%2Fpr%2Fgwkh%2Fdzgwkh%2Fgr_dzgwkh_index.jsp&gotoOnFail=http://www.urp.fudan.edu.cn/portal?.flag=fail';
PostStr:='Login.Token1=04538&Login.Token2=*******&Submit=%B5%C7+%C2%BC';
target:=NULL;
flag:=0;
PostLen := Length(PostStr);
// 用构建 varByte 类型的 Variant array
Post := VarArrayCreate([0, (PostLen - 1)], varByte);
// 填充数据
for Index := 0 to PostLen - 1 do
     Post[Index] := Ord(PostStr[Index + 1]);
// 填补头部数据注意:application
Header := 'Content-Type: application/x-www-form-urlencoded';

WebBrowser1.Navigate2(URL,flag,target,POST,Header);
while WebBrowser1.Busy=True do
      Application.ProcessMessages ;
if WebBrowser1.ReadyState =READYSTATE_COMPLETE    then
Memo1.Text :=WebBrowser1.OleObject.Document.all.tags('HTML').Item(0).outerHTML;
else
Memo1.Text:='页面加载失败';
end;


end.

注意:Navigate2里的参数必须定义成OleVariant类型,并且必须使用变量进行传递参数,不能直接在里面赋常量值!

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

21 − 16 =