【问题标题】:Connection Closed Gracefully requesting from last.fm using Indy TIdHTTP使用 Indy TIdHTTP 从 last.fm 请求正常关闭的连接
【发布时间】:2014-07-06 01:51:49
【问题描述】:

我正在尝试使用 Indy 的 TIdHTTP 组件来使用 last.fm api。当我发送请求时,我一直收到Connection closed gracefully,但没有收到任何数据。但是,当我将相同的 URL 从组件(直接复制到剪贴板)复制到 Chrome 或 Firefox 等网络浏览器时,它可以正常工作。

我正在尝试实现 album.search 调用,并正在尝试 GET 此示例 URL 中的数据:

http://ws.audioscrobbler.com/2.0/?format=json&api_key=MY_API_KEY&method=album.search&album=believe&limit=30&page=1

This request is documented here

我有一个辅助函数来连接常见的 URL 结构:

function TLastFm.ApiUrl(const Method: String): String;
begin
  Result:= Format('http://ws.audioscrobbler.com/%s/?format=json&api_key=%s&method=%s',
    [FVersion, FKey, Method]); //FVersion = '2.0', FKey = my API key
end;

然后我像这样进行实际调用:

var
  S, R: String;
begin
  S:= ApiUrl('album.search')+'&album='+Album; //Album = 'believe'
  S:= S + '&limit=30&page=1';
  Clipboard.AsText:= S; //Used to paste into browser to test
  FHTTP.Request.UserAgent:= 'Mozilla/3.0 (compatible; JD Test)';
  R:= FHTTP.Get(S); //<-- Connection closed gracefully
  // ...
end;

如何使用 Indy 的 TIdHTTP 成功拨打电话?

【问题讨论】:

  • 您的程序发送的 HTTP 请求与浏览器发送的请求有何不同?我会使用像 Wireshark 这样的数据包嗅探器来检查。我认为现在可能有更专业的程序。
  • 对 Firefox 行为建模的高级检查的一个很好的例子是 Firebug 扩展。
  • Fiddler 很好用,Wireshark 也不错。

标签: delphi indy10 last.fm


【解决方案1】:

我刚刚创建了一个帐户并获得了我的 API 密钥。我设置了这些常量:

const
  FVersion = '2.0';
  FKey = 'MYAPIKEY';
  Album = 'believe';
function ApiUrl(const Method: String): String;

我在表单上设置了 IdHTTP 组件,但您手动创建它。

procedure TForm1.btnGetRequestClick(Sender: TObject);
var
  S, R: String;
begin
  S := ApiUrl('album.search')+'&album='+Album; //Album = 'believe'
  S := S + '&limit=30&page=1';
  IdHTTP1.Request.UserAgent := 'Mozilla/3.0 (compatible; JD Test)';
  mem.Lines.Text := IdHTTP1.Get(S);
end;

我收到了一份包含 json 结果的备忘录。

编辑:

我做了一些后续测试,如果搜索关键字中有空格,您将收到连接关闭错误。

如果你尝试:

Album = 'believe '; // Connection closed gracefully

如果你尝试:

Album = 'believe'; // I get a json response back

【讨论】:

  • Face+Palm... 我在输入搜索词的文本框中有一个空格。我还没有进入 URL 格式化部分,因为我不知道我不小心在那里有一个空格。
猜你喜欢
  • 2010-11-08
  • 2015-03-08
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多