繁体   English   中英

如何使用Telerik REST API从C#控制台应用程序请求报告

[英]How to request report from c# console application using Telerik REST API

我有Telerik REST API,在客户端,我正在使用html5 report-viewer 报表在html报表中成功生成。 现在,我想通过c#控制台应用程序从相同的API请求报告。 我有搜索,但没有任何解决方案。 请给我建议一种如何使用C#控制台应用程序请求报告的方法。

html5报告查看器库

注意:我是telerik报告的初学者。

更新1:

我已经设法使用此API文档将请求发送到服务器。 Telerik获取报告的文档

在服务器端,我编写了CustomReportResolver 但是现在它现在将InstanceId发送到控制台客户端。

CustomReportResolver

public class CustomReportResolver : IReportResolver
    {
        public ReportSource Resolve(string reportJsonString)
        {
            var reportDto = JsonConvert.DeserializeObject<ReportDTO>(reportJsonString);

            var connectionStringHandler = new CustomConnectionStringManager(reportDto.CompanyId);


            var reportsPath = HttpContext.Current.Server.MapPath($"~/Reports/{reportDto.ReportPath}");


            var sourceReportSource = new UriReportSource { Uri = reportsPath  + reportDto.ReportName };

         //   sourceReportSource.Parameters.Add(new Telerik.Reporting.Parameter("companyId", reportDto.CompanyId));
            var reportSource = connectionStringHandler.UpdateReportSource(sourceReportSource);
            return reportSource;
        }
    }

请注意,如果我使用默认的ReportResolver自托管的telerik服务成功将pdf报告发送到控制台,但是如果我使用CustomReportResolver则不会生成instanceId

可能是什么问题呢 ?

浪费大量时间后,找到了一种解决方案,该解决方案如何从Telerik Self托管Web服务获取PDF (或其他报告格式)文档。 以下是要遵循的一般步骤。

  1. 获取客户ID
  2. 获取实例ID
  3. 取得文件编号
  4. 下载文件

下面是一个逐步的代码:

static HttpClient client = new HttpClient();
        static string reportServerAddress = "http://localhost:60031/";
        static string serverREStAPI = reportServerAddress + "api/";

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Demo started");

                RunAsync().Wait();

                Console.WriteLine("Demo ended");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadKey();
            }
        }

        static async Task RunAsync()
        {
           // readFile();
           // return;
            client.BaseAddress = new Uri(serverREStAPI);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));



            /*
             * Steps To get PDF documents from Telerik Self hosted Web Service
             * Step 1) Get Client Id,
             * Step 2) Get Instance Id
             * Step 3) Get Document Id
             * Step 4) Download Document
             * 
             * */

            var clientId = await GetClientIdAsync(serverREStAPI + "reports/clients", "clientId");

            var instanceId =
                await GetInstanceAsync(serverREStAPI + $"reports/clients/{clientId}/instances", "instanceId");
            var documentId =
                await GetDocumentAsync(serverREStAPI + $"reports/clients/{clientId}/instances/{instanceId}/documents",
                    "documentId");

              await DownloadPDF(serverREStAPI + $"reports/clients/{clientId}/instances/{instanceId}/documents/{documentId}", true);


        }





        static async Task<string> GetClientIdAsync(string path, string paramName)
        {

            HttpResponseMessage response = await client.PostAsJsonAsync(path, "");
            response.EnsureSuccessStatusCode();

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result);
            return result[paramName];
        }

        static async Task<string> GetInstanceAsync(string path, string paramName)
        {

            /*
             * For Default resolver in Service
             * */
            var paramterValues = new {CompanyId = 1};
            // var data = new { report = "{ \"ReportName\":\"test.trdx\",\"CompanyId\":\"1\"}", parameterValues = "{\"CompanyId\": \"1\"}" };    
            var data = new
            {
                report = "{\"ReportName\":\"test.trdx\",\"CompanyId\":\"1\"}",
                parameterValues = paramterValues
            };





            HttpResponseMessage response = await client.PostAsJsonAsync(path, data);
            response.EnsureSuccessStatusCode();

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result);
            return result[paramName];
        }

        static async Task<string> GetDocumentAsync(string path, string paramName)
        {



            var data = new {format = "PDF"}; //PDF,XLS,MHTML
            HttpResponseMessage response = await client.PostAsJsonAsync(path, data);
            response.EnsureSuccessStatusCode();

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            dynamic result = serializer.DeserializeObject(response.Content.ReadAsStringAsync().Result);
            return result[paramName];
        }



        static async Task DownloadPDF(string path, bool asAttachment)
        {
            var queryString = "";

            // if (asAttachment)
            //  {
            //  queryString += "?content-disposition=attachment";
            //  }



            var filePathAndName = @"D:\testing\tet.html";
            //   File.Create(filePathAndName);

            //  string filePath = System.IO.Path.Combine(folderName, fileName);

            //System.IO.File.WriteAllText(filePathAndName, result);


            using (System.Net.WebClient myWebClient = new System.Net.WebClient())
            {
                await myWebClient.DownloadFileTaskAsync(new Uri(path + queryString), filePathAndName);
            }



            System.Diagnostics.Process.Start(filePathAndName);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM