IdHTTP.不等待回答

HwT*_*rap 0 delphi indy delphi-2010 idhttp

我在使用Idhttp.Get方法时遇到了一些问题.我认为它默认工作在阻塞模式(等待答案转到下一行),但我只是看到它不等待答案转到另一行.我正在使用它与线程,但我认为这不是问题.代码是:

  IdHTTP1:= TIdHttp.Create(Application);
  IdHTTP1.ConnectTimeout:= 10000;
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.BasicAuthentication:= true;
  IdHTTP1.Request.Username := Username;
  IdHTTP1.Request.Password := Password;
    try
      IdHTTP1.Get(PbxURL); **//this should STOP here and wait for answer don't?**
      HttpCode:= IdHTTP1.ResponseCode;
    except
      on E: EIdHTTPProtocolException do
        HttpCode := IdHTTP1.ResponseCode;
    end;
    if HttpCode=200 then
      Memo1.Lines.Append('Valid Get!');
Run Code Online (Sandbox Code Playgroud)

所以,我只是注意到我没有得到正确的HttpCode值,因为在'Get'方法之后,它只是继续执行而不等待'Get'完成.我怎么解决这个问题??

Rob*_*edy 5

你说你没有得到正确的HttpCode,这表明你正在得到一个HttpCode,这意味着只要它需要得到一个结果Get方法已经等待.

如果您获得的响应代码是301,那么您应该尝试设置HandleRedirects属性,以便它将使用返回的地址自动重新发出请求.否则,您必须自己处理响应.

Get函数不会过早返回.你误解了你所观察到的东西.