본문 바로가기
Delphi, RadStudio

[개발/delphi] How can I pass PostData when I Navigate to a URL

by SB리치퍼슨 2012. 1. 25.
[개발/delphi] 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;
반응형

댓글