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

5lhxktic  于 2024-01-07  发布在  其他
关注(0)|答案(1)|浏览(309)

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

  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3. var apiURL := 'https://api.github.com/users/octocat';
  4. try
  5. var IdHTTP := TIdHTTP.Create;
  6. try
  7. var jsonResponse := IdHTTP.Get(apiURL);
  8. Memo1.Lines.Text := jsonResponse;
  9. finally
  10. IdHTTP.Free;
  11. end;
  12. except
  13. on E: Exception do
  14. ShowMessage('Error: ' + E.Message);
  15. end;
  16. end;

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

o4tp2gmn

o4tp2gmn1#

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

  1. implementation
  2. uses
  3. Idhttp, IdSSLOpenSSL;
  4. {$R *.dfm}
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. begin
  7. var apiURL := 'https://api.github.com/users/octocat';
  8. try
  9. var IdHTTP := TIdHTTP.Create;
  10. try
  11. var ssl := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
  12. ssl.SSLOptions.SSLVersions := [sslvTLSv1_2];
  13. IdHTTP.IOHandler := ssl;
  14. var jsonResponse := IdHTTP.Get(apiURL);
  15. Memo1.Lines.Text := jsonResponse;
  16. finally
  17. IdHTTP.Free;
  18. end;
  19. except
  20. on E: Exception do
  21. ShowMessage('Error: ' + E.Message);
  22. end;
  23. end;

字符串

展开查看全部

相关问题