Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-15
5
Medium Priority
?
1,024 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 336 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 332 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 332 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

927 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