[英]How to use HTTPS in Delphi 10.3 with INDY
大家好,所有的程序員,
目前我想弄清楚,如何在 Delphi 中使用 HTTPS 請求 (POST)。 我有這段代碼:
uses ..., IdHTTP, IdSSLOpenSSL;
....
procedure TForm2.Button4Click(Sender: TObject);
var
PostStrings :TStringList;
ResponseStream :TStringStream;
ausgabe :String;
zeile :String;
i :Integer;
s :array of string;
Strings :TStringList;
Stream :TStringStream;
begin
Stream := TStringStream.Create('');
Strings := TStringList.Create;
try
Strings.Add('Noone='+Edit1.Text);
Strings.Add('Foo='+Edit2.Text);
IdHTTP1 := TIdHTTP.Create(nil);
try
IdHTTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP1);
IdHTTP1.HandleRedirects := True;
idHTTP1.Post('https://api.mysitefortest.de/postit.php', Strings,stream);
ausgabe := Stream.DataString;
zeile := '';
for i:=1 to Length(ausgabe) do begin
if (ausgabe[i]=#13) and (ausgabe[i+1]=#10) then begin
ResponseMemo.Lines.Add(zeile);
zeile:='';
end else if s[i]<>#10 Then zeile:=zeile+ausgabe[i];
end;
ResponseMemo.Lines.Add(zeile);
except
on E: Exception do
showmessage('Error encountered during POST: ' + E.Message);
end;
finally
Strings.Free;
Stream.Free;
end;
end;
php文件:
<?php
echo 'Noone: '.$zitatArray["Noone"];
echo Chr(13).Chr(10);
echo 'Foo: '.$zitatArray["Foo"];
?>
如果我按下 sendpost 按鈕,就會出現錯誤。 它說,無法加載 ssl.-library。
MMhhh - 如何在 indy 中使用 ssl 似乎是一個秘密。 我搜索過谷歌,但沒有找到一些明確的例子。 我想我需要一個 ssl 庫。 但哪一個? 如何在我的 Delphi 中使用它? 從哪里得到它?
希望有人可以幫助和解釋它 - 也為其他程序員解決這個問題?
謝謝
(對不起,如果我的代碼不清楚 - 我還在學習;))
我懷疑您可能沒有安裝 SSL 庫。 使用 OpenSSL(在此處安裝https://slproweb.com/products/Win32OpenSSL.html )
這是使用 Indy 10 使用 SSL/TLS 1.2 發布帶有標頭的正文的示例
設置連接
uses
SysUtils, Classes, uTAuthToken, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
IdSSL,IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdIntercept, IdHTTP, IdCoder, IdCoder3to4, IdCoderMIME;
var
fIdHTTP: TIdHTTP;
fIdSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
fIdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
with fIdSSLIOHandlerSocketOpenSSL.SSLOptions do begin
Method := sslvTLSv1_2;
Mode := sslmClient;
SSLVersions := [sslvTLSv1_2];
end;
fIdHTTP := TIdHTTP.Create(nil);
if aConnectTimeout>100 then
fIdHTTP.ConnectTimeout := aConnectTimeout;
if aReadTimeout>100 then
fIdHTTP.ReadTimeout := aReadTimeout;
fIdHTTP.IOHandler := fIdSSLIOHandlerSocketOpenSSL;
if aProxyServer<>'' then begin
with fIdHTTP.ProxyParams do begin
ProxyServer := aProxyServer;
ProxyPort := aProxyPort;
end;
end;
發布帶有授權標頭的正文
aResponse := '';
aError := '';
aParams := TStringStream.create(aMessage);
try
with fIdHTTP do begin
Request.Clear;
Request.CustomHeaders.Clear;
Request.CustomHeaders.Add(Format('Authorization: Bearer %s',[access_token]));
Request.ContentType := 'application/json';
Response.KeepAlive := False;
aResponse := Post(aUri, aParams);
aResponse := aResp;
end;
finally
aParams.Free;
end;
HTH
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.