1

I'm making a Delphi XE5 VCL Forms Application and I'm trying to connect to a server using TIdHTTP. There is the code of my procedure:

procedure SendData(url: string; parameters: TStringList);
var 
  client: TIdHTTP;
  IdSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
  responceStream: TStringStream;
begin
  client := TIdHTTP.Create(nil);

  try
    IdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    try
      responceStream := TStringStream.Create;
      try
        client.IOHandler := IdSSLIOHandler;
        client.Post(url, parameters, responceStream);
        memo.Lines.Add(client.Post(url, parameters, responceStream));
      finally
        responceStream.Free;
      end;
    finally
      IdSSLIOHandler.Free;
    end;
  finally
    client.Free;
  end;
end;

The message I get is: 'HTTP/1.1 200 OK' but what I'm trying to receive as response is the HTML of the page to which server sends as response. Any ideas how can I do that.

bob_saginowski
  • 1,429
  • 2
  • 20
  • 35
  • What is the expected answer? – Filipe.Fonseca May 28 '14 at 11:06
  • Expected: OK or ERR(plus description) – bob_saginowski May 28 '14 at 11:07
  • Every overload of `TIdHTTP.Post` is a procedure, which means it returns no value. You're using it as if it does (in `memo.Add.Lines(client.Post());`. The code you've posted should not even compile because of that fact, which leads me to suspect you've not posted real code here. – Ken White May 28 '14 at 12:59
  • @KenWhite you mean that memo.Add.Lines(client.Post()); is invalid. This compiles and the response is 'HTTP/1.1 200 OK' but my question is how to get the content of the page server sends as response – bob_saginowski May 28 '14 at 15:25
  • Yes, `memo.Add.Lines` is invalid, as would be `memo.Lines.Add` - neither one of them would compile. `TMemo` has no `Add` method or property, and even if it did **procedures don't return a value**. I can't answer your question until you [edit] to provide actual, compilable code so we can tell what your real issue is; posting made up, non-working code is the same as posting no code at all. The code you've posted cannot possibly compile. – Ken White May 28 '14 at 15:28
  • @KenWhite just edited it – bob_saginowski May 28 '14 at 15:37
  • http://stackoverflow.com/questions/18832081/tidhttp-character-encoding-of-post-response Indy 10 has an overloaded version of TIdHTTP.Post() that returns a String (first answer) – bob_saginowski May 28 '14 at 15:49
  • @KenWhite the parameters argument is TStringList – bob_saginowski May 29 '14 at 08:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54705/discussion-between-mitko-berbatov-and-ken-white). – bob_saginowski May 29 '14 at 09:23

1 Answers1

2

Try those:

Uses
  IdBaseComponent, IdTCPConnection, IdTCPClient, IdHTTP, IdComponent, StrUtils;

function PostData(const AURL: string; AParamList: TStrings): string;
var
  _idHTTP: TIdHTTP;
begin
  _idHTTP := TIdHTTP.Create(nil);
  try
    _idHTTP.Request.UserAgent :=
      'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0';
    _idHTTP.Request.ContentType :=
      'application/json, text/javascript, */*; q=0.01';
    _idHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(_idHTTP);
    Result := _idHTTP.Post(AURL, AParamList);
  finally
    FreeAndNil(_idHTTP);
  end;
end;

function PostData(const AURL: string; AParamList: TStringList): string;
var
  _idHTTP: TIdHTTP;
  _ResultStream: TStringStream;
  _IdSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  _ResultStream := TStringStream.Create('', TEncoding.UTF8);
  _idHTTP := TIdHTTP.Create(nil);
  _IdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  try
    _idHTTP.IOHandler := _IdSSLIOHandler;
    _idHTTP.Request.UserAgent :=
      'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0';
    _idHTTP.Request.ContentType :=
      'application/json, text/javascript, */*; q=0.01';
    _idHTTP.Post(AURL, AParamList, _ResultStream);
    _ResultStream.Position := 0;
    Result := _ResultStream.DataString;
  finally
    FreeAndNil(_ResultStream);
    FreeAndNil(_idHTTP);
    FreeAndNil(_IdSSLIOHandler);
  end;
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
var
  _ParamList: TStringList;
begin
  _ParamList := TStringList.Create;
  try
    _ParamList.Add('paramname1=param_value1');
    _ParamList.Add('paramname2=param_value2');
    Memo1.text := PostData('http://someurl.com', _ParamList);
  finally
    FreeAndNil(_ParamList);
  end;
end;

Should be easy to add to memo as functions returns string.

Edijs Kolesnikovičs
  • 1,627
  • 3
  • 18
  • 34