Solved

open a new web browser window and submit form as 'POST' method

Posted on 2006-07-15
5
855 Views
Last Modified: 2010-04-05
Hi, every one.
Just want to know:
how to open a new web browser window from Delphi and submit form as 'POST' method?

Thanks
0
Comment
Question by:wangzheng
5 Comments
 
LVL 5

Accepted Solution

by:
Scay7 earned 84 total points
Comment Utility
Hi there you should check out SHELLEXECUTE

http://delphi.about.com/od/windowsshellapi/a/executeprogram.htm

It can launch an external program of file with parameters, mabey if you get the website POST method link you could SHELLEXECUTE it directly...

Peace Scay7
0
 
LVL 1

Assisted Solution

by:Tueblo
Tueblo earned 83 total points
Comment Utility
Do you really need the browser to open? If not you can just call a command line tool that posts data to the URL.
You could use the open source freeware cURL (http://curl.haxx.se/).
I think option -d is what you look for. Here you can find the option page: http://curl.haxx.se/docs/manpage.html

Regards !
Tueblo
0
 
LVL 4

Assisted Solution

by:LMuadDIb
LMuadDIb earned 83 total points
Comment Utility
what about opening IE with the results? Using a TWebBrowser component:

  function HTTPEncode(const AStr: string): string;
  procedure SaveAsHTML(WB:TWebBrowser; const FileName : string);

uses
      SHDocVw, ActiveX, ShellAPI;

...

procedure TForm1.btn1Click(Sender: TObject);
var
  EncodedDataString: string;
  PostData: OleVariant;
  Headers: OleVariant;
  i: integer;
begin { TForm1.Button1Click }
  // First, create a URL encoded string of the data
  EncodedDataString := 'UserName='+HTTPEncode('MyName')+'&'+
                       'UserPass='+HTTPEncode('MyPassword');

  // The PostData OleVariant needs to be an array of bytes
  // as large as the string (minus the 0 terminator)
  PostData := VarArrayCreate([0, length(EncodedDataString)-1], varByte);

  // Now, move the Ordinal value of the character into the PostData array
  for i := 1 to length(EncodedDataString) do
    PostData[i-1] := ord(EncodedDataString[i]);

  Headers := 'Content-type: application/x-www-form-urlencoded'#10#13;

  // Parameters 2 and 3 are not used, thus EmptyParam is passed.
  WebBrowser1.Navigate('http://www.mydomain.com/scripts/login.asp',
                       EmptyParam, EmptyParam, PostData, Headers);


end; { TForm1.Button1Click }

procedure SaveAsHTML(WB:TWebBrowser; const FileName : string);
var
  PersistStream: IPersistStreamInit;
  Stream: IStream;
  FileStream: TFileStream;
begin
  if not Assigned(WB.Document) then
  begin
    ShowMessage('Document not loaded!');
    Exit;
  end;

  PersistStream := WB.Document as IPersistStreamInit;
  FileStream := TFileStream.Create(FileName, fmCreate);
  try
    Stream := TStreamAdapter.Create(FileStream, soReference)
              as IStream;
    if Failed(PersistStream.Save(Stream, True)) then
      ShowMessage('SaveAs HTML fail!');
  finally
    FileStream.Free;
  end;
end; (* WB_SaveAs_HTML *)

function HTTPEncode(const AStr: string): string;
const
  NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
var
  Sp, Rp: PChar;
begin
  SetLength(Result, Length(AStr) * 3);
  Sp := PChar(AStr);
  Rp := PChar(Result);
  while Sp^ <> #0 do
  begin
    if Sp^ in NoConversion then
      Rp^ := Sp^
    else if Sp^ = ' ' then
      Rp^ := '+'
    else
    begin
      FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
      Inc(Rp, 2);
    end;
    Inc(Rp);
    Inc(Sp);
  end;
  SetLength(Result, Rp - PChar(Result));
end;

procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
begin
  SaveAsHTML(WebBrowser1,'c:/temp/tmp.htm');
  if FileExists('c:/temp/tmp.htm')  then
        ShellExecute(Handle, 'open', PChar('C:/temp/tmp.htm'), nil, nil, SW_SHOW);
end;

...
the above POST will submit a username/password to a website. You can change it to POST any params.
But you must check the html FORM on your website and pass the appropiate parameters. Look for the FORM INPUTs.

  EncodedDataString := 'aInput1='+HTTPEncode('someparamfor1')+'&'+
                       'aInput2='+HTTPEncode('someparamfor2');

just remember to put a '&' between each parameter passed in the POST string
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now