Recent

Author Topic: [SOLVED]Detect that file is on server with fphttpclient?  (Read 3171 times)

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
[SOLVED]Detect that file is on server with fphttpclient?
« on: February 13, 2017, 02:09:02 pm »
Hi. Is there a way to detect, if a file exists on the server, with fphttpclient?   :(

Thanks in advance
Bob
« Last Edit: February 13, 2017, 02:59:32 pm by Bob the Swede »
Rob

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Detect that file is on server with fphttpclient?
« Reply #1 on: February 13, 2017, 02:36:23 pm »
You could use HEAD to see if the page (or file) exists.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses fphttpclient, Classes;
  3. var
  4.   Fp: TFPHttpClient;
  5. begin
  6.   Fp := TFPHttpClient.Create(nil);
  7.   try
  8.  
  9.     Fp.HTTPMethod('HEAD', 'http://www.microsoft.com/I_DO_NOT_EXIST.html', nil, []);
  10.     Writeln(Fp.ResponseStatusCode); // will give 404
  11.  
  12.     Fp.HTTPMethod('HEAD', 'http://www.microsoft.com/nl-nl/windows', nil, []);
  13.     Writeln(Fp.ResponseStatusCode); // will give 200
  14.  
  15.   finally
  16.     Fp.Free;
  17.   end;
  18.   readln;
  19. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Detect that file is on server with fphttpclient?
« Reply #2 on: February 13, 2017, 02:47:08 pm »
That is correct.

Note ONLY for files that are in the public part of your server.
That code can not detect if a file exists anywhere outside of the serve-able space on that server disk.
In other words, you can not detect the presence of a specific file if it is not serve-able.
« Last Edit: February 13, 2017, 02:48:41 pm by Thaddy »
Specialize a type, not a var.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Detect that file is on server with fphttpclient?
« Reply #3 on: February 13, 2017, 02:57:08 pm »
It also doesn't account for redirection. Lot's of file-services use redirection for files. In that case you will always get a 3xx redirect on every possible filename. So if that's the purpose you need to do some manual work to follow the redirection to its final source and see if it throws an error-page.

So, if you need more information you will also need to provide us with more information.

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Re: [SOLVED]Detect that file is on server with fphttpclient?
« Reply #4 on: February 13, 2017, 02:59:00 pm »
This is what I was looking for. :)
Have a great day.
/Bob
Rob

 

TinyPortal © 2005-2018