使用Delphi调用oData服务

10

我该如何在Delphi中使用oData webservice(我正在尝试与新的Bing Search Azure API交互)?几乎没有有关在Delphi中执行此操作的信息。这里有一个帖子(链接),但它并没有很好地解释从Delphi角度如何使用此类服务。是否有人能够提供一个简单的示例?

1个回答

4

以下是一个使用 Netflix oData 服务在 Delphi XE 中消费 oData 服务的非常简单的示例:

program oDataDemo;

{$APPTYPE CONSOLE}

uses
  SysUtils, msxml, Variants, Activex;

var
  httpRequest: IXMLHttpRequest;
  oDataServiceURI: String;
  oDataFilter: String;
  xmlResults: String;
begin
  try
    oDataServiceURI := 'http://odata.netflix.com/v2/Catalog/Titles()';
    oDataFilter := '?$top=10';
    coinitialize(nil);
    httpRequest := CoXMLHTTP.Create;
    httpRequest.open('GET', UTF8Encode(oDataServiceURI + oDataFilter), false, EmptyParam, EmptyParam);
    httpRequest.send(EmptyParam);
    xmlResults := httpRequest.responseText;
    WriteLn(xmlResults);

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

应该使用URLEncode()而不是UTF8Encode(oDataServiceURI + oDataFilter)吗? - mjn
1
我会研究一下URLEncode,我的理解是URI应该是UTF-8编码的,请参考这个链接:https://dev59.com/i3NA5IYBdhLWcg3wjOve#913653以及这里关于当前标准的部分:http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding%5Fin%5Fa%5FURI - Mattl
所有使用Delphi和Rest的示例都是这种格式。xmlResults作为文本的用途是什么?如果Delphi要求程序员手动解析Atom pub xml,那么Delphi实际上并不支持Rest。它只支持HttpRequests。XE3是否真正解决了这个问题? - reckface
1
我猜这取决于你的RESTful服务返回什么,Delphi中解析XML和JSON都非常容易。对于JSON,我通常使用一个叫做SuperObject的第三方库来轻松解析它。 - Mattl
我还没有找到在Delphi中解析OData的简单方法。即使是Remy在 https://dev59.com/JmAf5IYBdhLWcg3wSRHi 中提供的出色的JSON示例也不能用于OData。 - SiBrit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接