DELPHI下POST GET数据和取得网页源文件
DELPHI下POST GET数据和取得
网页源文件
DELPHI下POST/GET数据和取得网页源文件(3种方法)2010-06-16 21:52方法一:利用TWebBrowser组件unit Unit1;interface uses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Di
alogs,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:='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.
方法二:利用XMLHTTP组件use comobj,ActiveX,var xmlHTTP:oleVariant;code0:OleVariant;begin code0:
=CODE+'&DataGrid1%3A_ctl'+inttostr(strtoint(form1.Edit2.Text)+1)+'%3A
zhj1=on';while not connectfalg do begin try CoInitialize(nil);
xmlHttp:=CreateOleObject('Msxml2.XMLHTTP');except CoUninitialize();
Continue;end;
try begin;xmlHttp.open('POST',Url,false);
xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xmlHttp.send(code0);htmlcode:=xmlHttp.responsetext;
if xmlHttp.status='200'then begin connectfalg:=True;Result:=0;
xmlHttp:=Unassigned;CoUninitialize();Exit;end;end;except on E:
Exception do begin ShowMessage(e.Message);end;end;end;Result:=-1;
end;
方法三:TIdhttp组件use IdHTTP Function Getwebs():Integer;
stdcall;//线程函数Var myhttp:TIdhttp;param:string;begin myhttp:
=Tidhttp.Create(nil);myhttp.ReadTimeout:=30000;try begin cc:
=myhttp.Get(param);End;except begin myhttp.Free;Result:=1;Exit;
end;end;Result:=0;end;
补充一例:WinInet函数相关DWORD dwConnectState;CString
strConnectState;BOOL
bOK=InternetGetConnectedState(&dwConnectState,0);
if(bOK){if(dwConnectState&INTERNET_CONNECTION_LAN)strConnectState="Local system uses alocal area network to connect to the Internet.";
if(dwConnectState&INTERNET_CONNECTION_MODEM)strConnectState="Local system uses amodem to connect to the Internet.";
if(dwConnectState&INTERNET_CONNECTION_MODEM_BUSY)strConnectState="No longer used.";
if(dwConnectState=INTERNET_CONNECTION_PROXY)strConnectState="Local system uses aproxy server to connect to the Internet.";}测试连接是否
有效,可以用:InternetCheckConnection检测计算机是否联网比较简单的做
法可以通过一个Win32 Internet(WinInet)函数InternetCheckConnection来
实现;这个函数的功能是检查是否能够建立Internet连接。它的实现是
在%SystemRoot%\System32\wininet.dll中,Delphi调用声明在WinInet.pas
中,其API声明如下:BOOL InternetCheckConnection(IN LPCSTR lpszUrl,IN
DWORD dwFlags,IN DWORD dwReserved);参数的意义是:lpszUrl:需要测试能否建立连接的URL地址,可以为空;dwFlags:目前只能是
FLAG_ICC_FORCE_CONNECTION(这个常量Delphi中没有声明,其值为
dwReserved:目前只能为0。调用的
:如果lpszUrl[message]000001);
是非空的,Windows从中解析出Host名然后Ping这个指定的Host。如果lpszUrl是空的,并且WinInet内部服务器的database中有一个关于最近的Server的纪录,Windows就从这条纪录中解析出Host名然后Ping它。如果能够成功的连接返回True,否则返回False;以下是一个判断当前计算机是否联网的例子:procedure TForm1.Button1Click(Sender:TObject);begin if InternetCheckConnection('1,0)then edit1.text:='Connected'else edit1.text:='Disconnected';end;通过上述的方法只能检测出当前计算机是否物理联网,即网线是否接好,网卡是否能顺利工作,不能确定是否能够实现获得Internet服务,即是否能和ISP进行Internet连接。这时可以通过另一个Win32 Internet(WinInet)函数InternetQueryOption来检测;这个函数的功能是查询指定Internet句柄的状态、选项。其API声明如下:BOOL InternetQueryOption(IN HINTERNET hInternet,IN DWORD dwOption,OUT
LPVOID lpBuffer,IN OUT LPDWORD lpdwBufferLength);参数的意义是:hInternet:查询对象的Internet句柄(全局查询时为nil),dwOption:查询的项目;lpBuffer:返回的查询结果;lpdwBufferLength:查询结果的字节长度(包括IN和OUT);查询成功返回True,否则返回False;我们要查询当前计算机的Internet连接状态时可以使用查询项目
INTERNET_OPTION_CONNECTED_STATE,得到的ConnectState返回值可能是以下值的一个或几个值之和:INTERNET_STATE_CONNECTED:[message]000001连接状态;INTERNET_STATE_DISCONNECTED:[message]000002非连接状态(和INTERNET_STATE_CONNECTED对应);INTERNET_STATE_DISCONNECTED_BY_USER:[message]000010用户请求的非连接状态INTERNET_STATE_IDLE:[message]000100连接状态,并且空闲INTERNET_STATE_BUSY:
[message]000200连接状态,正在响应连接请求以下是一个判断当前计算机是否可以获得Internet服务的例子:function TForm1.CheckOffline:boolean;var ConnectState:DWORD;StateSize:DWORD;begin ConnectState:=0;StateSize:=SizeOf(ConnectState);result:=false;if InternetQueryOption(nil,INTERNET_OPTION_CONNECTED_STATE,@ConnectState
,StateSize)then if(ConnectState and INTERNET_STATE_DISCONNECTED)2
then result:=true;end;procedure TForm1.Button1Click(Sender:TObject);begin if CheckOffline then edit1.text:='Connect To ISP'else edit1.text:='Disconnect To ISP';end;需要说明的是InternetQueryOption函数的检测结果只能
明当前的Internet设置是可用的,并不能表示计算机一定能访问Internet,例如网线掉了,网卡突然坏了之类的错误就没法检测出来,要想检测当前计算机是否能够获得Internet服务了必须两个函数结合起来使用。