Solved

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

Posted on 2006-07-15
5
946 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month6 days, 23 hours left to enroll

623 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