【发布时间】:2014-07-09 00:29:44
【问题描述】:
我正在尝试使用 Delphi XE3 和 Indy10 编写一个使用 Mashape API 服务的客户端应用程序,但我遇到了一些障碍。
这是我尝试过的:
我在表单上放置了TIdHTTP 和TIdSSLIOHandlerSocketOpenSSL 组件,并使用TIdHTTP.IOHandler 属性将它们链接在一起。然后我在我的表单上放置了一个按钮和备忘录,并在按钮OnClick event 上放置了以下代码:
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 做同样的事情。
【问题讨论】: