Autor Beitrag
Nightwalker_Z
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Di 01.02.05 09:07 
Hallo,
ich habe folgende Funktion geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function TForm1.isHTTPSourceThere(Url:String):integer;
var iCode:integer;
begin
  iCode:=-1;
  try
    IdHTTP.Head(Url);
    iCode:=IdHTTP.ResponseCode;
    // Result=200 --> Erfolgreich
  except
    iCode:=IdHTTP.ResponseCode;
    ShowMessage(IdHTTP.ResponseText);
  end; //except
  Result:=iCode;
end;


Diese Funktion soll über einen HTTP Head Request das vorhandensein einer URL prüfen. Falls eine URL nicht verfügbar ist (HTTP 404) springt man in die Exception.
Jetzt mein Problem:
Wenn ich eine fiktive URL überprüfen lasse, die es bestimmt nicht gibt, springt man in die Exception ABER der Responsecode ist nicht 404, sondern 200 (OK) :shock:
Auch der Responsetext sagt: HTTP1.1 / 200 OK
Was hab ich denn jetzt falsch gemacht??

Moderiert von user profile iconUGrohne: Code- durch Delphi-Tags ersetzt.
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: Di 01.02.05 10:53 
Welche URL hast du denn benutzt, denn bei mir klappt es!
Nightwalker_Z Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Di 01.02.05 11:05 
Hmmm... bei Dir kommt als Errorcode 404 raus :x
Doof :-)

Komisch... hab grad mein Delphi gestartet und mein Projekt geöffnet. Seit dem letzten mal hab ich keine Änderung gemacht und jetzt funzt es :oops:
Ich versteh die Welt nicht mehr.

Danke für die Hilfe *schnieeef*
Nightwalker_Z Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Di 01.02.05 13:54 
Jetzt klappts wieder nicht.
Wenn ich diesem Link verwende:
www.mirrorservice.or...ox%20Setup%201.0.exe

springt das Programm zwar in die exception (Socket Error # 11002), aber schmeisst mir als iCode 200 raus (HTTP1.1 200 OK)

Das kann doch gar nicht sein !!! *Heul*
sakura
ontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 137

W2KS, W2K3S
D1Pr, D3Pr, D4Pr, D5E, D7A, D8A, D2005A
BeitragVerfasst: Di 01.02.05 14:02 
Nightwalker_Z hat folgendes geschrieben:
aber schmeisst mir als iCode 200 raus (HTTP1.1 200 OK)

Warum nicht? Schließlich funktioniert der Link und man kann was runterladen.

_________________
Das Lächeln ist die eleganteste Art dem Gegner die Zähne zu zeigen.
Borland SE
Nightwalker_Z Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Di 01.02.05 15:20 
O.k... aber bei mir kommt sogar der Code 200, wenn ich den Link wirklich fies verbiege:

www.miice.org/sit/ft...ox%20Setup%201.0.exe

Also irgendwie sollte hier ein 404er zurückgegeben werden. Aber wie gesagt: beim HTTP.Head springt er bei diesem Link in ne Exception.
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: Di 01.02.05 16:28 
Alsoooo..

Als Code dürfte garnix kommen denn der Server, den es nicht gibt, kann dir auch nix zurückgeben,egal ob Code 200 oder 404 etc.

Du Solltest dann als Responsecode den Wert -1 bekommen. Der ist von Indy!!

Teste mal www.google.de, www.google123456.de, www.google.de/index.html, www.google.de/index123456.html

alles unterschiedliche ergebnisse!

mfg herzi
Nightwalker_Z Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Mi 02.02.05 08:01 
Stimmt!!! Hast vollkommen recht. Wenn der Server nicht existiert, kann ich auch keinen 404er zurückbekommen. Aber wie finde ich nun raus, ob ein Server existiert?

Bsp.: www.pahgfdggehl.de/arc_convert.zip

Diese URL bringt mir bei der Funktion oben einen 200er zurück!!!

warum das denn?
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Mi 02.02.05 08:37 
ausblenden Delphi-Quelltext
1:
IdHTTP1.Response.Clear;					


Das solltest du mal verwenden! Und du hast im Exceptions-Block noch die Möglichkeit eine Behandlungroutine einzubauen. EIdHTTPProtocolException scheint wohl nur mit Get zu funktionieren. Aber EIdSocketError geht.

_________________
Ciao, Sprint.
Nightwalker_Z Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Mi 02.02.05 10:38 
So - jetzt hab ich folgenden Ansatz. Der will aber auch nicht so ganz klappen :(

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:
function TForm1.isHTTPSourceThere(Url:String):integer;
var iCode:integer;
    boServerThere:boolean;
begin
  IdHTTP.Response.Clear;
  boServerThere:=true;
  // Serververfügbarkeit überprüfen:
  try
    IdHTTP.Head(Url);
  except
    on E:EIdSocketError do
    begin
      iCode:=-1;
      boServerThere:=false;
      ShowMessage('Server Not There');
    end;
  end;
  if boServerThere then
  begin
  // Dateiverfügbarkeit überprüfen
    try
      IdHTTP.Head(Url);
      iCode:=IdHTTP.ResponseCode;
      // Result=200 --> Erfolgreich
    except
      iCode:=IdHTTP.ResponseCode;
      ShowMessage('File Not There');
    end;
  end;
  Result:=iCode;
end;


Sodele... wenn ich jetzt ne URL mit vollkommenen blödsinn eingebe (Bsp.: www.goooooogle3455.de/index.html), passt das schon. Er springt in die erste Exception und bringt mir ein Message-Fenster (Server not there). Btw.: Das ist ein Socket Error #1101 (Host not found).

Angenonnen ich übergebe jetzt ne URL mit einem Existierenden Server, aber ohne existierendes File, sollte das erste Exception zwar triggern, aber nicht den Except Teil durchlaufen, da es kein EIdSocketError ist.
--> Die Variable boServerThere bleibt true und das Programm sollte weiter in die Dateiverfügbarkeit laufen.

Kann ich das so machen? irgendwie läuft mein Programm nicht in den zweiten Try..Except (Dateiprüfungsteil) rein
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Mi 02.02.05 11:14 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function TForm1.isHTTPSourceThere(const AURL: String): Integer;
begin

  with IdHTTP1 do
  begin
    try
      Response.Clear;
      Head(AURL);
    except
    end;
    Result := IdHTTP1.ResponseCode;
  end;

end;


Gibt -1 zurück, wenn Server nicht erreichbar ist. 404, wenn Resource nicht gefunden werden kann und 200 wenn alles okay war.

_________________
Ciao, Sprint.
Nightwalker_Z Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Mi 02.02.05 11:41 
Das ist leider falsch.
Habs gerade mit folgender URL ausprobiert: mesddddh.dl.sourcefo.../sevenzip/7z415b.exe

Nach der Zeile: Result := IdHTTP.ResponseCode;
Füg ich als Debugging folgendes ein:
ShowMessage(inttostr(IdHTTP.ResponseCode));

Dummerweise bekomm ich beim Link oben einen Popup in dem 200 steht und nicht -1.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Mi 02.02.05 13:50 
Tut mir leid. Ich weißt nicht, was du da am PC machst.

_________________
Ciao, Sprint.
herzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 168

WINXP pro, SUSE Linux 9.0
D7 Pers.
BeitragVerfasst: Mi 02.02.05 14:29 
Also mit der Funktion funktioniert alles so wie du es haben willst(zumindest bei mir)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function TForm1.isHTTPSourceThere(const AURL: String): Integer;
var
  idhttp1:TIDhttp;
begin
  idhttp1 := Tidhttp.Create(nil);
  with IdHTTP1 do
  begin
    try
      Response.Clear;
      HEAD(AURL);
    except
    end;
    Result := IdHTTP1.ResponseCode;
  end;
  idhttp1.Free;
end;