Solved

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

Posted on 2006-07-15
5
866 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Thread safe  opinion 7 126
Delphi application Soap connection 5 96
Dev Express grid collapse 2 39
PHP preg_replace code convert to Delphi 14 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

911 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

18 Experts available now in Live!

Get 1:1 Help Now