TidHTTP.Post question...

Okay, i have this forum at http://www.workshop-alex.org/ which is quite uninteresting. It's just for experimental purposes anyway. You can have a look, even register but don't think it's a very reliable site...

Oh, well... I want to do something special with my own site. I want to auto-post messages in the 'Announcements' and the 'Jokes' groups. And I want to do this from a Delphi application! Thus I have a Delphi client and this client will first have to log on, maintain a cookie, go to the group and add a new topic to this group. Unfortunately, the amount of data I want to add in this new post could be too big to add on the URL of my site, thus I have to use the Post method to send the post to my server.

now, of course I could use a completely different technique, even include the post directly in the database on my server, but this is just a challenge for me. I want to do it this way and this way only. So, how do I get this to work? And I do want to use Indy for this...
LVL 17
Wim ten BrinkSelf-employed developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VGRCommented:
no problem, even using NMHTTP from D4

The only tricky part is to maintain the cookie.

I rather suggest you directly talk to the DB server - less challenging but more adapted to Delphi web applications IMHO
Wim ten BrinkSelf-employed developerAuthor Commented:
Well, as you say... Talking to the DB server is just less of a challenge... And Indy has a nice component to maintain cookies which I've already played with. It works perfectly, allowing me to log in using the get method and specifying the parameters in the URL. But this doesn't work when the parameters are more than 10.000 characters...
So I have to find out how this post thingie works...
Eddie ShipmanAll-around developerCommented:
Here is how to post data using Indy (Posts to an asp page)

procedure TForm1.SendPostData;
Var
  aStream: TMemoryStream;
  Params: TStringStream;
begin
  aStream := TMemoryStream.create;
  Params := TStringStream.create('');

  try
    with IdHTTP1 do
    begin
      Params.WriteString(URLEncode('teste=' + 'yes' + '&'));
      Params.WriteString(URLEncode('name=' + 'ivan' + '&'));
      Params.WriteString(URLEncode('number=' + '102'));
      Request.ContentType := 'application/x-www-form-urlencoded';
      try
        Post('http://localhost/teste.asp', Params, aStream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;
  aStream.WriteBuffer(#0' ', 1);
  aStream.Position := 0;
  Memo1.Lines.LoadFromStream(aStream);
  except
  end;
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wim ten BrinkSelf-employed developerAuthor Commented:
@EddieShipman: Close enough for me! It works after a minor adjustment. Don't know where URLEncode comes from but doesn't matter. In the unit HTTPApp I found HTMLEncode() which seems to do the same. However, this led to:
  Params.WriteString(HTMLEncode('username=WorkshopAlex')+'&');
since I have to keep the '&' outside the encoding. Not a big thingie...

However, the magic word I needed was:
  IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
Which I had forgotten...

Btw... I just used:
  List := TStringList.Create;
  List.Text := IdHTTP1.Post('http://www.neopets.com/login.phtml', Params);
To get the page inside a stringlist. That works just as well as using that stream method you used. :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.