Einzelnen Beitrag anzeigen

nytaiceman

Registriert seit: 15. Dez 2005
Ort: Schweiz, Bern
58 Beiträge
 
Delphi XE3 Professional
 
#4

AW: idHttp und Verwendung der URL

  Alt 21. Sep 2016, 19:41
Ich hoffe das Leerzeichen in der URL ist nur ein Copy&Paste Fehler zum Eintrag ins Forum.

Folgender Codeschnippsel zeigt Dir die Fehlermeldung an (Bei mir ist kein ShowMessage im Einsatz sondern ein Logfile Manager). Ausserdem habe ich eine Funktion um einen Zufallspfad und Dateinamen zu generieren, aber ich denke den Sinn der Funktion ergibt sich und vielleicht hilft es Dir ja:

Delphi-Quellcode:
// === Get a filepath from HTTP ================================================
function GetHTTPfile(AQuery,AUser,APassword:String; var AResponseText:String; var AResponseCode:Integer; var AMimeType:String) : String;
// uses idHTTP, IdSSLOpenSSL, idURI, IdException, IdStack,
// System.SysUtils, System.StrUtils, Classes
var
 IdHTTP1 : TidHTTP;
 LHandler : TIdSSLIOHandlerSocketOpenSSL;
 lStream: TStringStream;
 httpGetString, sFilename : String;
 NewGUID: TGUID;
begin
 IdHTTP1 := TIdHTTP.Create(nil);
 IdHTTP1.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';

 lStream := TStringStream.Create(Result);

 with IdHTTP1 do
 begin
  LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  IOHandler := LHandler;
  ReadTimeout := 30000;
// OnWork := // Add here event handler
// OnWorkBegin := // Add here event handler

// = Check for authentification
  if (length(AUser) < 1) or (length(APassword) < 1) then Request.BasicAuthentication := False else
  begin
    Request.BasicAuthentication := true;
    HTTPOptions := HTTPOptions +[hoInProcessAuth];
    Request.Username := AUser;
    Request.Password := APassword;
  end;

    httpGetString := TIdURI.URLDecode(AQuery);
    httpGetString := TIdURI.URLEncode(httpGetString);

  try
    WriteLog(1,'HTTP','URL=' +httpGetString);
    IdHTTP1.Get(TIdURI.URLEncode(httpGetString), lStream);
    AResponseCode := IdHTTP1.ResponseCode;
    AResponseText := IdHTTP1.ResponseText;
    AMimeType := IdHTTP1.Response.ContentType;
    lStream.Position := 0;
    CreateGUID(NewGUID); sFilename := GUIDToString(NewGUID);
    sFilename := // -> Put here estimated temporary filepath
    lStream.SaveToFile(sFilename);
    Result := sFilename;

  except

    on E: EIdHTTPProtocolException do
    begin
      AResponseCode := IdHTTP1.ResponseCode;
      AResponseText := IdHTTP1.ResponseText;
      ShowMessage('[ERROR] ' +'Indy raised a protocol error!' +' | '
        +'HTTP status code: ' + IntToStr(E.ErrorCode) +' | '
        +'Error message' + E.Message);
    end;

    on E: EIdConnClosedGracefully do
    begin
      AResponseCode := IdHTTP1.ResponseCode;
      AResponseText := IdHTTP1.ResponseText;
      ShowMessage('[ERROR] ' +'Indy reports, that connection was closed gracefully!');
    end;

    on E: EIdSocketError do
    begin
      AResponseCode := IdHTTP1.ResponseCode;
      AResponseText := IdHTTP1.ResponseText;
      ShowMessage('[ERROR] ' +'Indy raised a socket error!' +' | '
        +'Error code: ' + IntToStr(E.LastError) +' | '
        +'Error message' + E.Message);
    end;

    on E: EIdException do
    begin
      AResponseCode := IdHTTP1.ResponseCode;
      AResponseText := IdHTTP1.ResponseText;
      ShowMessage('[ERROR] ' +'Indy raised an exception!' +' | '
        +'Exception class: ' + E.ClassName +' | '
        +'Error message: ' + E.Message);
    end;

    on E: Exception do
    begin
      AResponseCode := IdHTTP1.ResponseCode;
      AResponseText := IdHTTP1.ResponseText;
      ShowMessage('[ERROR] ' +'A non-Indy related exception has been raised!');
    end;

  end;

 end; {httpclient}

  LHandler.Free;
  IdHTTP1.Disconnect;
  IdHTTP1.Free;
  FreeAndNil(lStream);
end;
Einfach ist nur einfach, wenn Einfach auch einfach ist!
Vermeintlich einfache Workarounds führen irgendwann zu Problemen!
  Mit Zitat antworten Zitat