Obtenga la respuesta del servidor en TMemo como HTML

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.

preguntado el 28 de mayo de 14 a las 11:05

What is the expected answer? -

Expected: OK or ERR(plus description) -

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. -

@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 -

Sí, memo.Add.Lines is invalid, as would be memo.Lines.Add - neither one of them would compile. TMemo no tiene Add method or property, and even if it did procedures don't return a value. I can't answer your question until you editar 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. -

1 Respuestas

Prueba esos:

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.

Respondido el 08 de junio de 14 a las 10:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.