如何在 Delphi 中使用TIdHTTP动态获取JSON?

5lhxktic  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(171)

当我在Postman中对“https://api.github.com/users/octocat“进行GET请求时,它就可以工作了:
x1c 0d1x的数据
但是如果我尝试在 Delphi 中使用TIdHTTP使用以下代码来实现:

procedure TForm1.Button1Click(Sender: TObject);
begin
  var apiURL := 'https://api.github.com/users/octocat';
  try
    var IdHTTP := TIdHTTP.Create;
    try
      var jsonResponse := IdHTTP.Get(apiURL);

      Memo1.Lines.Text := jsonResponse;
    finally
      IdHTTP.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error: ' + E.Message);
  end;
end;

字符串
然后我得到一个错误:
项目引发了异常类EIdOSSLUnderlyingCryptoError,并显示消息“Error connecting with SSL. error:1409442 E:SSL routines:ssl3_read_bytes:tlsv 1 alert protocol version”
这是什么意思和/或我做错了什么?

o4tp2gmn

o4tp2gmn1#

你需要一个TIdSSLIOHandlerSocketOpenSSL来安装https,记住你需要openssl库。

implementation

uses
  Idhttp, IdSSLOpenSSL;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  var apiURL := 'https://api.github.com/users/octocat';
  try
    var IdHTTP := TIdHTTP.Create;
    try
      var ssl := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
      ssl.SSLOptions.SSLVersions := [sslvTLSv1_2];
      IdHTTP.IOHandler := ssl;

      var jsonResponse := IdHTTP.Get(apiURL);

      Memo1.Lines.Text := jsonResponse;
    finally
      IdHTTP.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error: ' + E.Message);
  end;
end;

字符串

相关问题