2010-11-19 9 views
15

Quelle est la manière la plus simple de faire une requête POST HTTPS dans Delphi? Je n'ai pas de problème avec les requêtes HTTP POST, mais comment puis-je le faire en utilisant SSL? J'ai fait un googled autour et n'ai rien trouvé qui explique cela assez bien.Comment faire une requête HTTPS POST en Delphi?

Voici le code que j'ai essayé:

procedure TForm1.FormCreate(Sender: TObject); 
var 
    responseXML:TMemoryStream; 
    responseFromServer:string; 
begin 
    responseXML := TMemoryStream.Create; 
    IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(self); 
    with idSSLIOHandlerSocketOpenSSL1 do 
    begin 
     SSLOptions.Method := sslvSSLv2; 
     SSLOptions.Mode := sslmUnassigned; 
     SSLOptions.VerifyMode := []; 
     SSLOptions.VerifyDepth := 0; 
     host := ''; 
    end; 

    IdHTTP1 := TIdHTTP.Create(Self); 
    with IdHTTP1 do 
    begin 
     IOHandler := IdSSLIOHandlerSocketOpenSSL1; 
     AllowCookies := True; 
     ProxyParams.BasicAuthentication := False; 
     ProxyParams.ProxyPort := 0; 
     Request.ContentLength := -1; 
     Request.ContentRangeEnd := 0; 
     Request.ContentRangeStart := 0; 
     Request.Accept := 'text/html, */*'; 
     Request.BasicAuthentication := False; 
     Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)'; 
     HTTPOptions := [hoForceEncodeParams]; 
    end; 
    responsefromserver := IdHTTP1.Post('https://.../','name1=value1&name2=value2&....'); 
end; 

Lorsque je tente de l'exécuter, je reçois l'erreur suivante:

Project myProject.exe raised exception class EFOpenError with message 'Cannot open file "C:\...\Projects\Debug\Win32\name1=value1name2=value2 The system cannot find the file specified'. 

Je ne comprends pas. J'ai envoyé des paramètres, bien que les erreurs semblent comme si j'aurais envoyé un fichier.

J'ai également inclus libeay32.dll et ssleay32.dll dans mon dossier myProject.exe.

+0

Avez-vous déjà trouvé un correctif pour cela? –

Répondre

3

Vous n'avez pas spécifié votre version de Delphi ou indy, mais j'ai eu quelques problèmes avec Indy avec Delphi 2009 et HTTPS, et quand j'ai eu la dernière source de indy svn, le problème a été résolu.

1

Une autre alternative à Indy est Synapse.

Cette bibliothèque de classes offre un contrôle total du poste, mais offre également une méthode simple post un liner ainsi:

function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean; 
+0

Votre fonction HttpPostURL prend-elle en charge https? – Ampere

1

http://chee-yang.blogspot.com/2008/03/using-indy-https-client-to-consume.html

var S: TStringList; 
    M: TStream; 
begin 
S := TStringList.Create; 
M := TMemoryStream.Create; 
try 
    S.Values['Email'] := 'your google account'; 
    S.Values['Passwd'] := 'your password'; 
    S.Values['source'] := 'estream-sqloffice-1.1.1.1'; 
    S.Values['service'] := 'cl'; 

    IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1; 
    IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded'; 
    IdHTTP1.Post('https://www.google.com/accounts/ClientLogin', S, M); 
    Memo1.Lines.Add(Format('Response Code: %d', [IdHTTP1.ResponseCode])); 
    Memo1.Lines.Add(Format('Response Text: %s', [IdHTTP1.ResponseText])); 

    M.Position := 0; 
    S.LoadFromStream(M); 
    Memo1.Lines.AddStrings(S); 
finally 
    S.Free; 
    M.Free; 
end; 

fin;

Questions connexes