Autor Beitrag
Nano-Ware
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Sa 05.12.09 20:04 
Hi,

sry wenn das das jetzt die falsche Kategorie ist^^

also:

Mein Problem ist:
  • Ich will prüfen, ob eine Datei auf einem Server vorhanden ist.
  • Der Server ist der Imageserver von Youtube.
  • Die Datei ist ein Bild


Ich möchte schaun, ob das Bild existiert. Es kann auch sein, dass evtl. der Server nicht existiert.

Das sind die Server, auf denen die Bilder liegen könnten:


Es könnte ja sein, dass Youtube auch irgendwann einmal noch Server hinzufügt, deshalb prüft er auch auf i5,i6,i7,i8,i9 und i10

Deshalb die Frage:

Gibt es eine Funktion, mit der man testen kann, ob eine Datei auf einem Server existiert, die aber auch funktioniert, wenn der Server bzw irgendein Unterordner nicht vorhanden ist?

Vielen Dank schon jetzt

PS: Ich bin noch ein Anfänger, aber die Grunderfahrung habe ich^^
Bob Murphy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 91

XP, Ubuntu
Delphi 7 SE, Delphi 2007
BeitragVerfasst: Sa 05.12.09 20:20 
Du könntest mit den Indys (IdHTTP) testen ob der Server erreichbar ist.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
try
  IdHTTP.Get('http://i1.ytimg.com/PFAD ZUM IMAGE');
  //Hier gehts weiter wenn der Server bzw die Datei vorhanden ist
except
  //Hier kommt das dann rein was gemacht werden soll wenn der Server nicht erreichbar bzw  die Datei nicht vorhanden ist
end;


Das ganze kannste dann in eine repeat Schleife packen, die dann alle Server durchgeht
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Sa 05.12.09 20:31 
Also wenn das Bild nicht vorhanden ist würde ein Fehler kommen, der wird abgefangen und dann gemerkt, ob die Datei vorhaden ist..^^ so einfach?


thx

PS: Weil ich angeschrieben wurde: Ich bin Delphi2009lover und hab mich umbenannt, weil ich einen neuen Namen wollte, weil ich ich jetzt Delphi 2010 hab :wink:
Bob Murphy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 91

XP, Ubuntu
Delphi 7 SE, Delphi 2007
BeitragVerfasst: Sa 05.12.09 20:36 
aber den pfad zur datei kennst du ja?
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Sa 05.12.09 20:40 
Ja
Bob Murphy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 91

XP, Ubuntu
Delphi 7 SE, Delphi 2007
BeitragVerfasst: Sa 05.12.09 20:53 
gut, dann kannst du es so machen, einfach eine repeat schleife in der mit idhttp überprüft wird ob die datei vorhanden ist. den vorgang wiederholst du solange bis die datei auf einen server gefunden wurde.
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Sa 05.12.09 21:20 
Leider funktioniert es doch nicht...

hier der Code:


ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
try

HT.Get('http://i4.ytimg.com/vi/KWJuDguYFIM/default.jpg');
Edit1.Text := 'OK';

except

Edit1.Text := 'NOT OK';

end;

try

HT.Get('http://i4.ytimg.com/vi/KWJuDguYFIM/default23456789.jpg');
Edit2.Text := 'OK';

except

Edit2.Text := 'NOT OK';

end;

try

HT.Get('http://i47777777.ytimg.com/vi/KWJuDguYFIM/default.jpg');
Edit3.Text := 'OK';

except

Edit3.Text := 'NOT OK';

end;


Wobei der 1. Link zu 100% geht. Warum zeigt er dann not ok an?
Bob Murphy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 91

XP, Ubuntu
Delphi 7 SE, Delphi 2007
BeitragVerfasst: So 06.12.09 09:39 
so solltest du das auch nicht machen, ich poste dir mal meinen code mit dem ich immer in meinen programmen überprüfe ob internet verfügbar ist. der code passt zwar nicht 100% auf dein problem, aber sollte kein problem sein das ein wenig umzuschreiben.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
function CheckInternet: Boolean;
var
  i: Integer;
  http: TIdHTTP;
const
  Service: array[0..5of string = (
    'http://www.google.com/',
    'http://www.myspace.com/',
    'http://www.facebook.com/',
    'http://www.ebay.com/',
    'http://www.microsoft.com/',
    'http://twitter.com/');
begin
  Result := false;

  http := TIdHTTP.Create(nil);
  http.HandleRedirects := true;
  i := 0;

  try
    repeat
      try
        http.Get(Service[i]);
        Result := true;
      except
        i := i + 1;
      end;

      if i = 6 then
      begin
        i := 0;
      end;
    until (Result = true);
  finally
    http.Free;
  end;
end;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 06.12.09 11:05 
Wozu denn gleich das Bild herunterladen? Es reicht doch den Header abzurufen, es sollte dann ein 404 zurückkommen, wenn die Datei nicht existiert.
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: So 06.12.09 11:33 
Gibt der auch 404 zurück wenn der Server nicht existiert?? Weil ich hätte gedacht, dass dann nur wieder Fehler kommen.

@Bob Murphy sry die Idee von Jaenicke ist besser :wink: weil deine Methode dauert schon recht lange... also Herr Jaenicke, wie soll ich das machen? Also wie ruf ich "Header" auf? Ich hab damit noch nie gearbeitet, ich weiß nur, dass es das gibt xD
Andreas L.
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1703
Erhaltene Danke: 25

Windows Vista / Windows 10
Delphi 2009 Pro (JVCL, DragDrop, rmKlever, ICS, EmbeddedWB, DEC, Indy)
BeitragVerfasst: So 06.12.09 12:24 
ausblenden Delphi-Quelltext
1:
  IdHTTp.head(URL);					
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: So 06.12.09 12:38 
Also ich wills jetzt verstehen^^ was gibt head denn eigentlich zurück?

Den Text zwischen <TITLE> und </TITLE>?
Andreas L.
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1703
Erhaltene Danke: 25

Windows Vista / Windows 10
Delphi 2009 Pro (JVCL, DragDrop, rmKlever, ICS, EmbeddedWB, DEC, Indy)
BeitragVerfasst: So 06.12.09 15:00 
Head gibt erstmal gar nichts zurück weil es eine procedure und keine Funktion ist. Die Routine fragt den Server nach den HTTP-Header. Um zu verstehen was das ist, muss man erst verstehen wie das HTTP-Protokoll funktioniert: de.wikipedia.org/wik...xt_Transfer_Protocol

Der abgefragte Header wird in der Eigenschaft Response abgelegt.

Ansonsten muss man sich natürlich noch die Indy-Doku anschauen: www.indyproject.org/...TTP_Head@string.html (man muss ein wenig warten bis sich das Topic öffnet)
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: So 06.12.09 15:16 
Sry was macht dann bitte die Procedure Head?
Niko S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: So 06.12.09 15:48 
Das hat doch Andreas geschrieben.
Schau dir mal IdHTTP.Response an dort findest du so einiges.
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: So 06.12.09 20:55 
Ich habs mit angeschaut, aber ich habe keine Ahnung, wie man diese Procedure aufruf und den Wert ausließt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 06.12.09 21:09 
user profile iconNano-Ware hat folgendes geschrieben Zum zitierten Posting springen:
Ich habs mit angeschaut, aber ich habe keine Ahnung, wie man diese Procedure aufruf
Du ersetzt Get durch Head... :autsch: :autsch: :autsch:

user profile iconNano-Ware hat folgendes geschrieben Zum zitierten Posting springen:
und den Wert ausließt
Du schaust wie bereits gesagt wurde in Response. Benutzen musst du Response.ResponseCode. Bzw. die Eigenschaft gibts auch schon parallel zu Response direkt in TIdHttp.
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: So 06.12.09 21:35 
Ich meinte das mit dem Response^^
Also wie ich das lösen muss^^

Ich hab keine Ahnung, wie das geht, wenn ich einfach:

IDHTTP1.Head(URL);

schreib zuckt sich das Programm mehrere Minuten nicht, bis ich es dann aufgebe
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 06.12.09 22:27 
Ich habe keine Ahnung was du machst, bei mir klappt das problemlos. :nixweiss:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var
  HttpLoader: TIdHttp;
begin
  HttpLoader := TIdHttp.Create;
  try
    HttpLoader.Head('http://i4.ytimg.com/vi/KWJuDguYFIM/default.jpg');
    if HttpLoader.ResponseCode = 200 then
      memResult.Lines.Add('erfolgreich');
  finally
    HttpLoader.Free;
  end;
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Mo 07.12.09 09:14 
Ich glaub ich habs etwas falsch geschrieben

ich habe den Code etwas verändert:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm6.Button1Click(Sender: TObject);
var
  HttpLoader: TIdHttp;
begin
  HttpLoader := TIdHttp.Create;
  try
    HttpLoader.Head(Edit1.Text);
    if HttpLoader.ResponseCode = 200 then
      memResult.Lines.Add('erfolgreich');
  finally
    HttpLoader.Free;
    memResult.Lines.Add('Finish');
  end;
end;


wenn ich jetzt wie beschrieben das: i4.ytimg.com/vi/KWJuDguYFIM/default.jpg "teste" gibt er erfolgreich aus, das ist schonmal gut, bei mir ging das nich so.

Das Problem ist ja, dass ich schauen will, ob eine Datei existiert, wenn ich jetzt nämlich im Edit

i4.ytimg.com/vi/KWJuDguYFIM/default.jp eingebe (also eine Datei, die nicht existiert) macht das Programm überhaupt nichts. Es bleibt dann einfach hängen

PS: Schaut euch mal auch den falschen Youtubelink im Browser an, denn da wird keine Error 404 Seite z.B. angezeigt sondern ein Bild


EDIT:// Das gleiche passiert übrigends auch wenn ich www.google.de eingebe, wobei ich immer gedacht hab die Seite existiert :wink:

EDIT2:// Ich hab den Code jetzt so abgeändert:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm6.Button1Click(Sender: TObject);

begin

  try
    IdHTTP1.Head(Edit1.Text);
    if IdHTTP1.ResponseCode = 200 then
      memResult.Lines.Add('erfolgreich');
 except

    memResult.Lines.Add('nope');
  end;
end;


Er reagiert genau so wie der andere. Wenn ich z.B. www.google.de eingebe (im Edit) dann dauerts 5 Minuten, dann gibt er "nope" aus, genauso wie bei dem falschen Youtubelink (*.jp) da dauerts auch sehr lange... Wenn ich aber den richtigen eingebe, erscheint innerhalb von ca 2 Sekunden "erfolgreich" woran liegt das jetzt???