2010-11-19 292 views
15

在Delphi中執行HTTPS POST請求的最簡單方法是什麼?我在製作HTTP POST請求時沒有問題,但我怎樣才能使用SSL?我一直在搜索,並沒有找到任何解釋得很好的東西。如何在Delphi中創建HTTPS POST請求?

這是我試過的代碼:

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; 

當我嘗試運行它,我得到以下錯誤:

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'. 

我不明白這一點。我發送參數,儘管錯誤聽起來像我會發送一個文件。

另外我還在myProject.exe文件夾中包含了libeay32.dll和ssleay32.dll。

+0

你有沒有找到解決這個問題? – 2017-02-15 14:30:11

回答

3

你沒有指定你的Delphi版本或indy版本,但是我在使用Delphi 2009和HTTPS捆綁Indy之前遇到了一些問題,當我從indy svn得到最新源碼時,問題就解決了。

1

另一個替代方案是印Synapse

這個類庫提供崗位的完全控制,而且還提供了一個簡單的襯墊POST方法,以及:

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

您的HttpPostURL函數是否支持https? – Ampere 2014-07-01 13:34:16

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; 

末;

相關問題