[개발/delphi] How can I pass PostData when I Navigate to a URL
Q : How can I pass PostData when I Navigate to a URL?
Q : How can I pass PostData when I Navigate to a URL?
A : I call the below method with a URL destination, PostData in the format of 'animal=cat&color=brown' etc. and the TWebBrowser object that I want to load the URL inside of...
procedure TDBModule.Navigate(stURL, stPostData: String; var wbWebBrowser: TWebBrowser);
var
vWebAddr, vPostData, vFlags, vFrame, vHeaders: OleVariant;
iLoop: Integer;
begin
{Are we posting data to this Url?}
if Length(stPostData)> 0 then
begin
{Require this header information if there is stPostData.}
vHeaders:= 'Content-Type: application/x-www-form-urlencoded'+ #10#13#0;
{Set the variant type for the vPostData.}
vPostData:= VarArrayCreate([0, Length(stPostData)], varByte);
for iLoop := 0 to Length(stPostData)- 1 do // Iterate
begin
vPostData[iLoop]:= Ord(stPostData[iLoop+ 1]);
end; // for
{Final terminating Character.}
vPostData[Length(stPostData)]:= 0;
{Set the type of Variant, cast}
TVarData(vPostData).vType:= varArray;
end;
{And the other stuff.}
vWebAddr:= stURL;
{Make the call Rex.}
wbWebBrowser.Navigate2(vWebAddr, vFlags, vFrame, vPostData, vHeaders);
end; {End of Navigate procedure.}
A: Here's another option:
procedure TForm1.SubmitPostForm;
var
strPostData: string;
Data: Pointer;
URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
begin
{
<!-- submit this html form: -->
<form method="post" action="http://127.0.0.1/cgi-bin/register.pl">
<input type="text" name="FIRSTNAME" value="Hans">
<input type="text" name="LASTNAME" value="Gulo">
<input type="text" name="NOTE" value="thats it">
<input type="submit">
</form>
}
strPostData := 'FIRSTNAME=Hans&LASTNAME=Gulo&NOTE=thats+it';
PostData := VarArrayCreate([0, Length(strPostData) - 1], varByte);
Data := VarArrayLock(PostData);
try
Move(strPostData[1], Data^, Length(strPostData));
finally
VarArrayUnlock(PostData);
end;
URL := 'http://127.0.0.1/cgi-bin/register.pl';
Flags := EmptyParam;
TargetFrameName := EmptyParam;
Headers := EmptyParam; // TWebBrowser will see that we are providing
// post data and then should automatically fill
// this Headers with appropriate value
WebBrowser1.Navigate2(URL, Flags, TargetFrameName, PostData, Headers);
end;
반응형
'Delphi, RadStudio' 카테고리의 다른 글
[개발/delphi] 다른 프로그램 버튼 클릭하기 (0) | 2012.02.07 |
---|---|
[개발/delphi] Indy 을 이용한 idHTTP POST 파일 전송 예제 (0) | 2012.01.25 |
[개발/delphi] 델파이 - Object Pascal 강좌 (0) | 2012.01.18 |
[개발/delphi] 인터넷 연결 목록을 추적하기 (0) | 2012.01.17 |
[개발/delphi] 델파이 zlib 를 이용한 파일압축 (0) | 2012.01.17 |
댓글