Delphi中POST方法获得网页源代码

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


unit Unit1;

interface

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

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://subin.org.cn/index.asp';
  PostStr:='测试POST字符串数据!'; 
  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;

  initialization
    OleInitialize(nil);
  finalization
    OleUninitialize;

end.

你可能感兴趣的:(Delphi中POST方法获得网页源代码)