![]() |
AW: Daten abholen von einem CakePHP Server
Zitat:
Wenn es JSON sein soll, das im Body gesendet wird, dann empfehle ich einen JSON Parser wie SuperObject zu verwenden. |
AW: Daten abholen von einem CakePHP Server
Zitat:
Daher die Frage, was er überhaupt senden soll. Fest steht nur, dass er vermutlich JSON zurückbekommt. |
AW: Daten abholen von einem CakePHP Server
Zitat:
Tipp: für das Debuggen kann man ganz einfach einen Interceptor in Indy zuweisen, der dann die Kommunikation protokolliert. Wenn das zu einfach ist ( :) ) kann man alternativ Fiddler2 als Proxy zum Loggen einsetzen. |
AW: Daten abholen von einem CakePHP Server
Ich würde mal sagen die Doku der API die ich erhalten habe ist noch "sehr basic". Die muss und werde ich im Anschluss selbst überarbeiten.
Die Idee mit dem JSON Superobjekt war mal wieder der Bringer überhaupt. Das Teil ist ja echt Weltklasse sobald man es mal halbwegs verstanden hat. -> Der Übergabe HTTPBody ist JSON -> und ich bekomme inzw. auch einen passenden JSON String als response zurück, welchen ich nun nur noch auswerten muss Bin absolut begeistert wie Klasse das funktioniert, muss jetzt aber erstmal etwas essen bevor ich das fertig mache. Ich poste dann vermutlich wieder meinen Code, damit andere auch etwas davon haben VIELEN DANK euch allen erneut LG MARTIN |
AW: Daten abholen von einem CakePHP Server
OK, here u go mit meinem Code zum Login
1.) wir erstellen uns einen record der die Anmeldedaten&Status beinhaltet um nachher einfacher darauf zugreifen zu können
Delphi-Quellcode:
2.) Die Funktion die den Login checkt und den record mit dem Anmeldedaten ausfüllt
TaCloudLogin = record
id :String; //returns the UID (User-ID) of the aCloud Username :String; Password :String; LogonSuccesfull :Boolean; PremiumStatus :Boolean; //does user get aditional access to functions like WebInterface etc. ? end; Var aCloudLogin :TaCloudLogin;
Delphi-Quellcode:
und hier noch ein Funktionsaufruf.....Function TForm1.Login2ACloud(aUsername,aPassword,aLocale,anActiveVersion :String; aPremiumStatus:Integer; IsSilentMode:Boolean):Boolean; Var iDHTTP: TIdHttp; Params: TStringStream; JSON: ISuperObject; begin if NOT ((Length(EaCloudUsername.Text) >= 5) AND (Pos('@', EaCloudUsername.Text) > 1 )) then begin Showmessage(Txt[715,L]); //invalid e-mail adress exit; end; IF (Length(EaCloudPassword.Text) < 4) then begin Showmessage(Txt[718,L]); //password missing or invalid exit; end; IdHttp := TIdHttp.Create; Params := TStringStream.create(''); //1.) Prepare and send a HTTP-Post to the CloudServer asking for LoggingIn try JSON := SO(); JSON['UID'] := SO('"' + CreateGuid + '"'); JSON['activeversion'] := SO(anActiveVersion); JSON['locale'] := SO(aLocale); JSON['password'] := SO(aPassword); JSON['premium'] := SO(aPremiumStatus); JSON['username'] := SO(aUsername); JSONParams := JSON.AsJSon(); Params.WriteString(JSONParams); iDHTTP.HandleRedirects := True; iDHTTP.Request.BasicAuthentication := True; iDHTTP.Request.Authentication := TIdBasicAuthentication.Create; iDHTTP.Request.Authentication.Username := EaCloudUsername.Text; iDHTTP.Request.Authentication.Password := EaCloudPassword.Text; IdHTTP.Request.ContentType := 'application/json'; IdHTTP.Response.KeepAlive := False; try JSONResponse := IdHTTP.Post(aCloudServer + 'users/login.json', Params); except on E: Exception do IF (IsSilentMode = FALSE) then showmessage('Error encountered during POST: ' + E.Message); end; finally IdHTTP.Free; end; //2.) Now we interprete if the connection was succesful and write data into the record "aCloudLogin" JSON := SO(JSONResponse); // Interprete the JSON response to the JSON Super-Object (SO) aCloudLogin.id := JSON.O['return'].S['id']; if (POS('OK', Uppercase(JSON.O['return'].S['response']) ) > 0) then aCloudLogin.LogonSuccesfull := True else aCloudLogin.LogonSuccesfull := FALSE; If (aPremiumStatus = 0) then aCloudLogin.PremiumStatus := True else aCloudLogin.PremiumStatus := FALSE; aCloudLogin.Username := aUsername; aCloudLogin.Password := aPassword; Result:= aCloudLogin.LogonSuccesfull; end;
Delphi-Quellcode:
Login2ACloud(EaCloudUsername.Text,EaCloudPassword.Text,'de', 'Aqua Calculator (V' + LVersionNr.Caption + ')', 1, True);
lG Martin |
AW: Daten abholen von einem CakePHP Server
Nachdem jetzt mit direktem www Zugang alles klappt, habe ich noch ein Thema sobald ich via Proxy verbinde
hier mal ein Stück Code das bei mir heraussucht welche Art von Proxy-Auth gemacht wird
Delphi-Quellcode:
procedure TForm1.IdHTTPProxyAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
begin Handled := False; end; function TForm1.IdSSLIOHandlerSocketOpenSSLVerifyPeer(Certificate: TIdX509; AOk: Boolean; ADepth, AError: Integer): Boolean; begin Result := True; end; procedure TForm1.IdHTTPSelectProxyAuthorization(Sender: TObject; var AuthenticationClass: TIdAuthenticationClass; AuthInfo: TIdHeaderList); begin ProxyAuthTxt := 'Proxy-Authentification: Unkown'; // First check for NTLM authentication, as you do not need to set username and password because Indy will automatically // handle passing your Windows Domain username and password to the proxy server if (pos('Proxy-Authenticate: NTLM', IdHTTP.Response.RawHeaders.Text)>0) then begin IdHTTP.ProxyParams.BasicAuthentication := false; AuthenticationClass := TIdSSPINTLMAuthentication; ProxyAuthTxt := 'Proxy-Authentific.: NTML (w/o Username+PW)'; ProxyAuthType := 1; end else begin //...now check for Basic Authentication if (pos('Proxy-Authenticate: Basic', IdHTTP.Response.RawHeaders.Text)>0) then begin IdHTTP.ProxyParams.BasicAuthentication := true; AuthenticationClass := TIdBasicAuthentication; ProxyAuthTxt := 'Proxy-Authentification: Basic'; ProxyAuthType := 2; end else begin // Then Digest if (pos('Proxy-Authenticate: Digest', IdHTTP.Response.RawHeaders.Text)>0) then begin IdHTTP.ProxyParams.BasicAuthentication := true; AuthenticationClass := TIdDigestAuthentication; ProxyAuthTxt := 'Proxy-Authentification: Digest'; ProxyAuthType := 3; end; end; //.------------ IdHTTP.ProxyParams.ProxyUsername := EProxyUsername.Text; IdHTTP.ProxyParams.ProxyPassword := EProxyPassword.Text; end; LProxyAutent.Caption := ProxyAuthTxt; end; HTTP Get funktioniert auch einwandfrei solange ich ein normales get/Post mache, "OHNE dem Server ein Username/Password für den Cloud-Zugriff mitzuübergeben" (URL, Port und Pasword/Username wegen das proxys sind natürlcih angegeben passen !) sobald ich den Cloud Username und Passwort mit angebe, bekomme ich beim "IdHttp.Get" eine Exception "HTTP/1.1 400 bad request" Gehe ich ich aber nicht über den Proxy, sondern verbinde direkt, dann funktioniert der Code eiwandfrei;
Delphi-Quellcode:
woran kann denn das liegen?
Function TForm1.Ask4aCloudResponse(aUsername, aPassword, aServerURL, aServerCommand: String ):String;
begin try IdHTTP.HandleRedirects := True; iDHTTP.Request.BasicAuthentication := True; iDHTTP.Request.Authentication := TIdBasicAuthentication.Create; iDHTTP.Request.Authentication.Username := aUsername; iDHTTP.Request.Authentication.Password := aPassword; IdHTTP.Request.ContentType := 'application/json'; Result := IdHttp.Get(aServerURL + aServerCommand); finally ; end; end; |
AW: Daten abholen von einem CakePHP Server
Einige Tage später und "der Knoten hat sich leider noch nicht gelöst"
Ich denke mein Problem ist das ich 2 Pärchen aus Username & Password habe a) Username & Password die ich brauche um mich bei einem "Basic" oder "Digest" Proxy zu authentifizieren (das müssten doch eigentlich sein: IdHTTP.ProxyParams.ProxyUsername / ProxyPassword wobei die ja zumindest bei NTML ja irgendwie duch Indy eingesetzt werden) b) Username & Password die dem Cloud-Server übergeben werden müssen um sich für den eigenen Cloud-Acount zu indetifizieren (das müssten doch eigentlich sein: iDHTTP.Request.Authentication.Username / Password) hat da jemand noch eine Idee die mich aus der völligen Desillusion befreit ? LG Martin |
AW: Daten abholen von einem CakePHP Server
Manchmal braucht man leider Ewigkeiten für eine Kleinigkeit.. hier war das der Fall
Ich habe den Fehler nach mehrfachem Kontakt zu Remy lebau (von Indy) und mittels 2 WireShark-Protokollen herausfinden können. Und zwar habe ich 1x das Protokoll meiner App gelogged und das mit dem Protokoll des IE verglichen (wo das ganze funktioniert hatte...) Mein Fehler war dass ich beim Übergabe-String so etwas geschickt hatte
Delphi-Quellcode:
der MS-IE hat das Kommando intern umgewandelt in
'measurements/changed.json?date=1970-01-01 01:01:00'
Delphi-Quellcode:
...und damit lief es
'measurements/changed.json?date=1970-01-01%2001:01:00'
Die Lösung ist also nichts weiter als ein
Delphi-Quellcode:
und alles ist gut
URLEncode('tanks/changed.json?date=1970-01-01 00:00:00')
LG Martin |
AW: Daten abholen von einem CakePHP Server
Zwecks Vollständigekit halber hier ein weiteres CodeFragment, da ich denke das es evtl. weiteren Personen hilft die mal etwas ähnliches machen wollen
Ziel: Ein FileUpload auf den Server mittels eines sog "Multipart Form DataStreams". So etwas wird zB zum Uploaden von Bildern eingesetzt lG Martin
Delphi-Quellcode:
Function TForm1.ACloud_UploadPicture(_PictureID, _OriPictureFPathAndName:String) : String;
Var POSTData: TIdMultipartFormDataStream; JSON: ISuperObject; begin Result := ''; try //generate a so called Multipart Object with //- the picture File itself. it is loaded as Stream form the path specified //- identifiers needed for the Cake server routines 'picture' , 'image/jpg' POSTData := TIdMultiPartFormDataStream.Create; POSTData .AddFile('picture', _OriPictureFPathAndName, 'image/jpg'); IDHTTP.HandleRedirects := True; IDHTTP.Request.BasicAuthentication := false; IDHTTP.Request.Authentication := TIdBasicAuthentication.Create; IDHTTP.Request.Authentication.Username := aCloudLogin.Username; IDHTTP.Request.Authentication.Password := aCloudLogin.Password; //although we handle the Multipart message, servers answers are again JSON for easier interpretation IdHTTP.Request.ContentType := 'application/json'; aCloudURL := URLEncode(aCloudServer + 'pictures/uplpic.json?PictureID=' + _PictureID+'.jpg'); JSONResponse := IDHTTP.Post(aCloudURL, POSTData); except on E : Exception do Result := Result + 'Exception uploading an image to aCloud: "' + E.ClassName + '" "' + E.Message +'"'; end; POSTData.Free; //--------------------------- if Result <> '' then exit; //--------------------------- //Now let's check if everythimng worked correctly. ...we interprte the Response returend from the Server //This is specific to your servers specifications and just an example on how it could be done JSON := SO(); JSON := SO(JSONResponse); // Interprete the response to the JSON Super-Object (SO) If (Uppercase(JSON.S['response']) <> Uppercase('OK') ) OR (Uppercase(JSON.S['entityID']) <> Uppercase(_PictureID+'.jpg')) OR (Uppercase(JSON.S['entityType']) <> Uppercase('picturedata') ) OR (Uppercase(JSON.S['type']) <> Uppercase('u') ) then Result := Result + 'Error responded updating an aCloud-picture: ' + JSONResponse; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:10 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz