Solved

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

Posted on 2006-07-15
5
902 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
ID: 17116879
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
ID: 17117368
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
ID: 17117608
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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