Board index » delphi » Can someone please send/post an example of using TIdHTTP Post on a site

Can someone please send/post an example of using TIdHTTP Post on a site

Can someone please send/post an example of using TIdHTTP Post on a site?

Idan

idan_co...@um.icomverse.com

 

Re:Can someone please send/post an example of using TIdHTTP Post on a site


This is a small example of using post to send data to web server.

There is two different ways to do this operation.

Example 1:
<----------------------------------------------------------------->
procedure TForm1.SendPostData;
Const
  CRLF = #13#10;
Var
  aStream: TMemoryStream;
  Params: TMemoryStream;
  S: String;
begin
  aStream := TMemoryStream.create;
  Params := TMemoryStream.Create;

  HTTP.Request.ContentType := 'multipart/form-data;
boundary=-----------------------------7cf87224d2020a';

  try
    S := '-----------------------------7cf87224d2020a' + CRLF +
      'Content-Disposition: form-data; name="sys_return_url"' + CRLF + CRLF
+
    'hello' + CRLF +
    '-----------------------------7cf87224d2020a--';

    Params.Write(S[1], Length(S));

    with HTTP do begin
      try
        HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
aStream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;
  aStream.WriteBuffer(#0' ', 1);
  showmessage(PChar(aStream.Memory));
  except
  end;
end;
<----------------------------------------------------------------->

Example 2:
<----------------------------------------------------------------->
procedure TForm1.SendPostData;
Var
  aStream: TMemoryStream;
  Params: TStringStream;
begin
  aStream := TMemoryStream.create;
  Params := TStringStream.create('');
  HTTP.Request.ContentType := 'application/x-www-form-urlencoded';

  try
    Params.WriteString(URLEncode('sys_return_url=' + 'helo' + '&'));
    Params.WriteString(URLEncode('sys_return_url=' + 'helo'));
    with HTTP do begin
      try
        HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
aStream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;
  aStream.WriteBuffer(#0' ', 1);
  showmessage(PChar(aStream.Memory));
  except
  end;
end;
<----------------------------------------------------------------->

As you can see there is a difference in the way post stream is constructed
and the ContentType. In the first example ContentType is
"multipart/form-data; boundary=-----------------------------7cf87224d2020a"
and this boundary is used to separate different parameters.

In the second example the ContentType is
"application/x-www-form-urlencoded".
In this case the paremeteras are passed in the form

ParamName=ParamValue&ParamName=ParamValue

Note that the Pramaeters in the second form must be URL encoded.

Where these two formats of post information are used?

The first one is used when you have binary data to post and the second one
is when you are going to post only text fields.

Doychin
doic...@5group.com

Quote
"Idan Cohen" <i_co...@yahoo.com> wrote in message

news:94qiec$kie7@bornews.inprise.com...
Quote
> Can someone please send/post an example of using TIdHTTP Post on a site?

> Idan

> idan_co...@um.icomverse.com

Re:Can someone please send/post an example of using TIdHTTP Post on a site


JJon...@Crosswinds.Net (Jorrit Jongma) wrote in <956an1$dkm2
@bornews.inprise.com>:

Quote
>Could you give an example on how to modify your first method to upload a
>file? (since this is also done with multipart/form-data)  I tried doing so
>but I seem to have fumbled it <g>

Indy does not support PUT nor binary POST yet. It will likely in 8.1.

--
Chad Z. Hower (Kudzu) - Church Hill, TN - Team Indy
      "Programming is an art form that fights back"
Forget the Y2K problem, Lets fix the W2K problem.
http://www.pbe.com/Kudzu/ - Free Delphi/CBuilder components and articles

Other Threads