如何通过C#调用IcCube Rest API执行ExecuteMdxScript接口?
提问:C#如何正确调用IcCube的ExecuteMdxScript接口
我已知晓可以通过执行FULL_LOAD_SCHEMA [{CubeName}]触发Schema加载,对应实现代码如下:
String postData; postData = $@"<?xml version=""1.0"" encoding=""UTF-8""?><soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" soap:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body><Execute xmlns=""urn:schemas-microsoft-com:xml-analysis""/><Command><Statement xsi:type=""xsd:string"">FULL_LOAD_SCHEMA [{CubeName}]</Statement></Command></Execute></soap:Body></soap:Envelope>";
我也可以通过IcCube Rest API测试IDE完成目标操作,如下图所示:
目前遇到的问题是:我需要在C# Web应用中配置对应接口端点和JSON参数来调用ExecuteMdxScript,我尝试了如下实现:
var url = "https://www.mySite.fr/icCube/xmla"; var httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "POST"; httpRequest.Accept = "application/json"; httpRequest.Headers["Authorization"] = "Basic aW1wb3J0X3NlcnZpY2VAcGtjcy5mcjpQQHNzdzByZDMkLVlzJDYlaFc="; httpRequest.ContentType = "application/json"; var data = @"{ ""schemaName"": ""Sales"", ""script"": ""create category hierarchy [Stats].[MaStatHierarchyAPI]"" }"; using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream())) { streamWriter.Write(data); } var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); } Console.WriteLine(httpResponse.StatusCode);
但调用后返回如下报错:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault xmlns="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>XMLAnalysisError.0x5496d617</faultcode><faultstring>unexpected processing error : Pretty print error!</faultstring><detail><Error ErrorCode="1419171351" Description="unexpected processing error : Pretty print error!"/></detail></soap:Fault></soap:Body></soap:Envelope>
我推测问题出在我不知道如何指定接口端点为"ExecuteMdxScript",当我把请求URL修改为:
var url = "https://www.mySite.fr/icCube/xmla/ExecuteMdxScript";
又返回了如下报错:
<h2>500 - Internal server error.</h2> <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
请问我该如何正确配置请求,在C#中调用IcCube的ExecuteMdxScript接口?
解答
你的核心错误是接口地址配置错误,ExecuteMdxScript属于icCube的REST API接口,不是XMLA接口,不能挂载在/icCube/xmla路径下。
正确的调用配置如下:
- 接口端点地址为
/icCube/api/rest/ExecuteMdxScript,可根据你的服务实际部署路径调整前缀 - 你原有的请求头、JSON参数结构符合接口要求,无需修改
修正后的完整代码:
// 替换为正确的服务地址,核心是/api/rest/ExecuteMdxScript路径 var url = "https://www.mySite.fr/icCube/api/rest/ExecuteMdxScript"; var httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "POST"; httpRequest.Accept = "application/json"; // 注意替换为你自己的有效Basic认证凭证 httpRequest.Headers["Authorization"] = "Basic aW1wb3J0X3NlcnZpY2VAcGtjcy5mcjpQQHNzdzByZDMkLVlzJDYlaFc="; httpRequest.ContentType = "application/json"; var data = @"{ ""schemaName"": ""Sales"", ""script"": ""create category hierarchy [Stats].[MaStatHierarchyAPI]"" }"; using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream())) { streamWriter.Write(data); } var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); } Console.WriteLine(httpResponse.StatusCode);
如果调整地址后仍有报错,请检查以下两项:
- 你使用的认证账号有对应Schema的脚本执行权限
- icCube服务的REST API功能已开启,没有被安全策略拦截
内容的提问来源于stack exchange,提问作者Bertrand Miot
相关产品推荐
相关产品推荐

