Variable result with IDHTTP

Asked

Viewed 2,239 times

1

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

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

It is possible to access this address via idHTTP, take this result and put in a Memo ? I know that via WININET is possible.

2 answers

1


It is possible yes.

Just use the method Get of IdHttp, thus:

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;
  • This method as is returns the source code of the page. If (and only if) the OP is using the latest version of Indy, it could get what you requested with the command http.Get(LINK, nil);, but for the method to work in older versions of Indy, I think it is better to use the head method.

  • @Sunstreaker, it did not work out, he of the Forbiden error, the address for true TEST is: http://www.escritoriocontatos.com.br/teste.php?LETRA

  • @Filipe.Fonseca, he gave Forbiden error in his too. See address http://www.escritoriocontatos.com.br/teste.php?LETRA

  • @user7605 test character only, temporarily remove your htaccess from folder / and try again.

  • @Fonseca, just removed and tested again, it appears in the showmessage the code 403. But I’ve checked everything, ta tudo certo....

  • @Sunstreaker the method still takes the "display source code" for Indy9. On reading, sensational workaround!

  • Perfect @Sunstreaker, thanks!

  • @Sunstreaker, this way we can make some test when the link falls it generate a message, type LINK OUT OF AIR!

Show 3 more comments

0

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

Follow example as requested 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 bring up the word OK, just make use of a case in the variable code. For this, follows HTTP response code list

Browser other questions tagged

You are not signed in. Login or sign up in order to post.