我正在尝试使用 Delphi XE3 和 Indy10 编写一个使用 Mashape API 服务的客户端应用程序,但我遇到了一些障碍。
这是我尝试过的:
我在表单上放置TIdHTTP
和组件,并使用该属性TIdSSLIOHandlerSocketOpenSSL
将它们链接在一起。TIdHTTP.IOHandler
然后我在表单上放置了一个按钮和备忘录,并在按钮OnClick
事件上放置了以下代码:
procedure TForm1.Button2Click(Sender: TObject);
begin
IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
IdHTTP1.Request.CustomHeaders.AddValue('X-Mashape-Key: ','<my_api_key>');
Memo1.Lines.Text := IdHTTP1.Get('https://hbrd-v1.p.mashape.com/anime/log-horizon');
end;
然后我启动我的应用程序,当我按下按钮时,应用程序将等待片刻,然后吐出一条HTTP/1.1 403 Forbidden
错误消息。第二次按下按钮将产生一条HTTP/1.1 500 Internal Service Error
消息。
我已经检查过我的系统上是否有所需的 SSL 库文件,并且确实有,并且我已经一次又一次地测试了我的凭据,它们似乎是正确的,而且我知道 url 是正确的,所以我一定是丢失了我的代码中的某些内容会导致显示这些错误。我希望在这方面有更多经验的人可以提供一些建议。
更新:这是TRESTClient
有效的代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IPPeerClient, Vcl.StdCtrls,
REST.Response.Adapter, REST.Client, Data.Bind.Components,
Data.Bind.ObjectScope;
type
TForm1 = class(TForm)
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
Button1: TButton;
Memo1: TMemo;
RESTResponse1: TRESTResponse;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
RESTClient1.BaseURL := 'https://hbrd-v1.p.mashape.com/anime/log-horizon';
RESTRequest1.Execute;
Memo1.Lines.Text := RESTResponse1.Content;
// The only thing not shown here is the RESTRequest1.Params which are
// Name: 'X-Mashape-Key'
// Value: 'my-api-key'
// Kind: pkHTTPHEADER
// Everything else is included here which isn't much.
end;
end.
我想对TIdHTTP
.