How to read response from Indy IdHTTP get

帅比萌擦擦* 提交于 2019-12-11 10:38:23

问题


I have written code to use get with indy IdHTTP component

var
get_url: string;
resp: TMemoryStream;
begin
  get_url := 'http://localhost/salesapp/is_run.php?ser=';
  resp := TMemoryStream.Create;

  IdHTTP1.Get(get_url+'v', resp);
  memo1.Lines.LoadFromStream(resp);

This url http://localhost/salesapp/is_run.php?ser=v return JSON response but I dont know how to read it from Delphi.


回答1:


When Get() exits, the stream's Position is at the end of the stream. You need to reset the Position back to 0 before calling LoadFromStream(), or else it will not have anything to load:

var
  get_url: string;
  resp: TMemoryStream;
begin
  get_url := 'http://localhost/salesapp/is_run.php?ser=';
  resp := TMemoryStream.Create;
  try
    IdHTTP1.Get(get_url+'v', resp);
    resp.Position := 0; // <-- add this!!
    memo1.Lines.LoadFromStream(resp);
  finally
    resp.Free;
  end;
end;

The alternative is to remove the TMemoryStream and let Get() return the JSON as a String instead:

memo1.Text := IdHTTP1.Get(get_url+'v');


来源:https://stackoverflow.com/questions/28462696/how-to-read-response-from-indy-idhttp-get

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!