Variable result with IDHTTP

1

I need to get the result of a link and put it in a memo. I'll explain better in examples:

I will access the link: http://www.xxx.com.br/teste.php?Teste (this address will return the word OK.

Is it possible to access this address via idHTTP, get this result and put it in a memo? I know that via WININET is possible.

    
asked by anonymous 25.06.2014 / 03:09

2 answers

1

Yes, it is possible.

Just use the Get method of IdHttp , like this: / p>

procedure TForm1.Button1Click(Sender: TObject);
 Const
   LINK = 'http://www.xxx.com.br/teste.php?Teste';
begin
   IdHTTP1.HandleRedirects := True;
   IdHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';

   Memo1.Text := IdHTTP1.Get(LINK);
end;
    
25.06.2014 / 04:47
0

Use the HEAD method, remembering that if the answer is not 200 (ok), the routine will fall into an exception and it should be handled.

Follow the example in the question:

procedure TForm1.Button1Click(Sender: TObject);
var
  http : TIdHttp;
  url : string;
  codigo : integer;
begin
  url := 'http://www.xxx.com.br/teste.php?Teste';
  http := TIdHTTP.Create(nil);
  try
    try
      http.Head(url);
      codigo := http.ResponseCode;
    except
      on E: EIdHTTPProtocolException do
        codigo := http.ResponseCode;
    end;
    ShowMessage(IntToStr(codigo));
  finally
    http.Free();
  end;
end;

Remembering that to make the word OK appear, just use a case in the code variable. To do this, follow the list of HTTP response codes

    
25.06.2014 / 14:40