简体   繁体   English

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

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

I have Telerik REST API and at client side i'm using html5 report-viewer . 我有Telerik REST API,在客户端,我正在使用html5 report-viewer Report are generating successfully in a report viwer in html. 报表在html报表中成功生成。 Now i want to request for the reports from same API through c# console application. 现在,我想通过c#控制台应用程序从相同的API请求报告。 I have search but didn't fine any solution. 我有搜索,但没有任何解决方案。 Please suggest me a way how can i request a report using C# console application. 请给我建议一种如何使用C#控制台应用程序请求报告的方法。

html5 report-viewer Library html5报告查看器库

Note: I'm very beginner in telerik reporting. 注意:我是telerik报告的初学者。

Update 1: 更新1:

I have manage to send request to the server using this API documentation. 我已经设法使用此API文档将请求发送到服务器。 Telerik Document for Getting Report Telerik获取报告的文档

on Server side i have written the CustomReportResolver . 在服务器端,我编写了CustomReportResolver But now its now sending the InstanceId to the console client. 但是现在它现在将InstanceId发送到控制台客户端。

CustomReportResolver 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;
        }
    }

Note if i use default ReportResolver self hosted telerik service sending the pdf report to console successfully but if i use CustomReportResolver it's not generating instanceId . 请注意,如果我使用默认的ReportResolver自托管的telerik服务成功将pdf报告发送到控制台,但是如果我使用CustomReportResolver则不会生成instanceId

What could be the problem ? 可能是什么问题呢 ?

After wasting a lot of time then found a solution how to get PDF (Or any other report format) documents from Telerik Self hosted Web Service. 浪费大量时间后,找到了一种解决方案,该解决方案如何从Telerik Self托管Web服务获取PDF (或其他报告格式)文档。 Following are the general steps to be followed. 以下是要遵循的一般步骤。

  1. Get Client Id 获取客户ID
  2. Get Instance Id 获取实例ID
  3. Get Document Id 取得文件编号
  4. Download Document 下载文件

Below is a step wise code: 下面是一个逐步的代码:

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.

相关问题 使用HttpClient()在C#控制台应用程序中对REST API的PUT请求 - PUT Request for REST API in C# console application using HttpClient() 如何从控制台应用程序调用 REST API? - How to call REST API from a console application? 从C#控制台应用程序迁移和运行REST API到ASP.NET - Migrating and Running REST API from C# Console Application to ASP.NET 如何使用Rest API从C#Windows应用程序对ALM进行身份验证 - How To authenticate ALM from C# Windows Application Using Rest API 从 C# 控制台应用程序启动 Crystal Report - Launch a Crystal Report from a C# console application 如何在C#中打开telerik报告文件并访问报告的数据源? - How to open the telerik report file in C# and access the DataSource of Report? C#Console Rest API标头有效负载 - C# Console Rest API header payload 如何在没有来自C#控制台应用程序数据的情况下向Web服务发送POST请求? - How to send a POST Request to a web service without data from console application in C#? 使用C#中的Oauth1从REST API获取请求令牌 - Obtain Request Token From REST API using Oauth1 in C# 使用ac#控制台应用程序从示例api获取数据时遇到问题 - Having trouble getting data from a sample api using a c# Console Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM