Search code examples
delphidelphi-xe2indy10

Get string from a idhttp get


currently I am able to run a command but i cant figure out how to get the result into a string.

I do a get like so

idhttp1.get('http://codeelf.com/games/the-grid-2/grid/',TStream(nil));

and everything seems to run ok, in wireshark i can see the results from that command. Now if i do

HTML := idhttp1.get('http://codeelf.com/games/the-grid-2/grid/');

it will freeze up the app, in wireshark i can see it sent the GET and got a response, but dont know why it freezes up. HTML is just a string var.

EDIT FULL CODE

BUTTON CLICK login(EUserName.Text,EPassWord.Text);

    procedure TForm5.Login(name: string; Pass: string);
var
 Params: TStringList;
 html : string;
begin
  Params := TStringList.Create;
  try
    Params.Add('user='+name);
    Params.Add('pass='+pass);
    Params.Add('sublogin=Login');


    //post password/username
     IdHTTP1.Post('http://codeelf.com/games/the-grid-2/grid/', Params);
    //get the grid source
     HTML := idhttp1.Get('http://codeelf.com/games/the-grid-2/grid/');

  finally
    Params.Free;
  end;
llogin.Caption := 'Logged In';
end;

RESPONCE The responce i get says Transfer-Encoding: chunked\r\n and Content-Type: text/html\r\n dont know if that matters. Thanks


Solution

  • Indy has support for some types of streamed HTTP responses (see New TIdHTTP hoNoReadMultipartMIME flag), but this will only help if the server uses multipart/* responses. The linked blog article explains the details further and also shows how the Indy HTTP component can feed a MIME decoder with a continuous response stream.

    If this is not applicable to your case, a workaround is to go down to the "raw" TCP layer, which means send the HTTP request using a TIdTCPClient component, and then read the response line by line (or byte by byte) from the IOHandler. This gives total control over response handling. Request and Response should be processed in a thread to decouple it from the main thread.