0

I'm having some problems with the Idhttp.Get method. I thought it's by default working in blocking-mode(wait for the answer to go to next line), but I just saw it's not waiting for the answer to go to another line. I'm using it with threads, but I think that's not the problem. The code is:

  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!');

So, I just notice that i'm not getting the right HttpCode value because after the 'Get' method, it just continue the execution without waiting for the 'Get' complete. How can I solve this problem??

HwTrap
  • 303
  • 1
  • 3
  • 14

1 Answers1

5

You say you're not getting the right HttpCode, which suggests you are getting an HttpCode, and that means the Get method has waited as long as it needed to to get a result.

If the response code you get is 301, then you should try setting the HandleRedirects property so it will automatically re-issue the request using the returned address. Otherwise, you'll have to handle the response yourself.

The Get function does not return prematurely. You're misinterpreting what you've observed.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I concur. Indy exclusively uses blocking sockets and blocking operations, so it is not possible for your code to continue until `Get()` has exited first, whether normally or via a raised exception. – Remy Lebeau Sep 16 '12 at 03:12
  • yes you are right. as I was working with threads, I created the IdHTTP in the wrong place, and it was working that way. Just changed the place where creating my IdHTTP object and it worked. Thank you. – HwTrap Sep 16 '12 at 15:37