从 C# 在 JasperServer 上运行报告
Jasper Reports 是 Crystal Reports 的绝佳开源替代品。它非常适合单页 PDF 页面,例如信件和电子邮件。发票到多页报告。然而,它对 .NET 不太友好,并且让 C#/Mono 与 JasperServer 很好地配合也没有取得成果。
有谁获得了如何从 C# 在 JasperServer 上运行报告并通过 SOAP 请求附加 XML 数据集的代码示例吗?它需要在 Mono 上运行,所以 Microsoft.Web.Services2 是不可能的。
我尝试着提出自己的肥皂请求。 Jasper 服务器似乎接受它,但除了服务器 500 错误之外,我似乎无法得到任何响应。我还没有附上 MTOM 附件。
var sb = new StringBuilder();
sb.AppendLine("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.AppendLine("<s:Body s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
sb.AppendLine("<q1:runReport xmlns:q1=\"http://axis2.ws.jasperserver.jaspersoft.com\">");
sb.AppendLine("<requestXmlString xsi:type=\"xsd:string\">");
sb.AppendLine("<request operationName=\"runReport\" locale=\"en\">");
sb.AppendLine(" <argument name=\"RUN_OUTPUT_FORMAT\">PDF</argument>");
sb.AppendFormat(" <resourceDescriptor name=\"\" wsType=\"\" uriString=\"{0}\" isNew=\"false\">", "/JourneyReport");
sb.AppendLine(" <label>null</label>");
sb.AppendLine(" <parameter name=\"testparam\">1</parameter>");
sb.AppendLine(" </resourceDescriptor>");
sb.AppendLine(" </request>");
sb.AppendLine("</requestXmlString>");
sb.AppendLine("</q1:runReport>");
sb.AppendLine("</s:Body></s:Envelope>");
var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/jasperserver/services/repository");
webRequest.Credentials = new NetworkCredential("jasperadmin","jasperadmin");
webRequest.PreAuthenticate = true;
webRequest.Headers.Add("SOAPAction","");
//Set HttpWebRequest properties
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;
webRequest.ContentType = "text/xml; encoding='utf-8'";
//Get Stream object
var objRequestStream = webRequest.GetRequestStream();
objRequestStream.Write(bytes, 0, bytes.Length);
objRequestStream.Close();
var response = (HttpWebResponse)webRequest.GetResponse();
Jasper Reports is a superb open source alternative to Crystal Reports. It's great for single page PDF pages such as letters & invoices to multi-page reports. However it's not very .NET friendly, and getting C#/Mono to play nice with JasperServer has not been fruitful.
Has anyone got any code samples of how to run a report on JasperServer from C#, and attach an XML dataset with the SOAP request? It needs to work on Mono, so Microsoft.Web.Services2 is out of the question.
I had a go at trying to roll my own soap request. Jasper Server seems to accept it, but I cant seem to get any response back other than a server 500 error. I didn't get as far as attaching a MTOM attachment.
var sb = new StringBuilder();
sb.AppendLine("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.AppendLine("<s:Body s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
sb.AppendLine("<q1:runReport xmlns:q1=\"http://axis2.ws.jasperserver.jaspersoft.com\">");
sb.AppendLine("<requestXmlString xsi:type=\"xsd:string\">");
sb.AppendLine("<request operationName=\"runReport\" locale=\"en\">");
sb.AppendLine(" <argument name=\"RUN_OUTPUT_FORMAT\">PDF</argument>");
sb.AppendFormat(" <resourceDescriptor name=\"\" wsType=\"\" uriString=\"{0}\" isNew=\"false\">", "/JourneyReport");
sb.AppendLine(" <label>null</label>");
sb.AppendLine(" <parameter name=\"testparam\">1</parameter>");
sb.AppendLine(" </resourceDescriptor>");
sb.AppendLine(" </request>");
sb.AppendLine("</requestXmlString>");
sb.AppendLine("</q1:runReport>");
sb.AppendLine("</s:Body></s:Envelope>");
var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/jasperserver/services/repository");
webRequest.Credentials = new NetworkCredential("jasperadmin","jasperadmin");
webRequest.PreAuthenticate = true;
webRequest.Headers.Add("SOAPAction","");
//Set HttpWebRequest properties
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;
webRequest.ContentType = "text/xml; encoding='utf-8'";
//Get Stream object
var objRequestStream = webRequest.GetRequestStream();
objRequestStream.Write(bytes, 0, bytes.Length);
objRequestStream.Close();
var response = (HttpWebResponse)webRequest.GetResponse();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Jasper 提供了一个 Web 服务 API,我想你已经找到了。对于使用 XML 的 Web 服务,当您将服务描述 (WSDL) 转换为该语言的服务存根时,可以通过任何语言(例如本例中的 C#)访问它。
在该给定链接上,可以找到 Jasper Reports wsdl 文件位置,访问它们后,您的任务是创建存根,这是对给定 XML 接口的代码级访问。对于 Mono,这可以根据本教程和其余部分使用简单的命令行命令来完成工作就是按照你想要的方式使用这个代码。
确切的命令可以通过这两个链接找到,没有太多魔力,但它就像使用给定路径运行
wsdl.exe
的命令一样简单(例如 http://localhost:8080/jasperserver/services/repository?wsdl) 作为参数,然后编译使用类似于mcs /target:library SomeService.cs -r:System.Web.Services
的命令得到的结果,其中将 SomeService.cs 替换为文件的名称是上一个命令的输出。就是这样!
Jasper gives a Web Services API which you already have found, I suppose. For that being a Web Services using XML, it can be accessed through any language, like C# in this case, when you convert the service description (WSDL) to a service stub on that language.
On that given link there can be found Jasper Reports wsdl file locations and after having access to them your task is to create the stub, which is a code level access to the given XML interface. For Mono this can be done with a simple command line command according to this tutorial and the rest of the work is to use this code how ever you want to use it.
The exact command can be found through these two links with not much magic, but it is something as easy as one command running
wsdl.exe
with the given path (eg. http://localhost:8080/jasperserver/services/repository?wsdl) as argument and then compiling the result with a command similar tomcs /target:library SomeService.cs -r:System.Web.Services
where you replace SomeService.cs with the name of the file that was the output of the previous command.That's it!
我遇到了同样的问题,不是在 Mono 中,而是在 Visual Studio 中。我总是收到错误 500。那是因为 jasperserver 根据 microsoft/mono 代码的答案不兼容 SOAP。 ASP.NET 需要一个 text/xml 结构,并且 jasperserver 发送回一个多部分结构,其中 xml 作为第一部分,报告作为第二部分中的附件。
ASP.NET 对此给出了例外。我现在尝试使用 REST 做类似的事情,但到目前为止我还没有成功。
添加时间:2012-03-09
弄清楚如何使用REST,请参阅使用 REST webservice 和 asp.net C# 从 jasperserver 获取报告
顺便说一下,这也适用于 Mono! (我在 Visual Studio 中开发,但在 Mono 上部署)
I ran into the same problem not in mono but using Visual Studio. I always get error 500. That's because the answer of jasperserver according to the microsoft/mono code is not SOAP complient. ASP.NET expects a text/xml structure, and jasperserver sends a multipart structure back with the xml as the first part and the report as an attachment in the second part.
ASP.NET gives an exception on that. I am now trying to do a similar thing using REST, but I've not succeeded so far.
Addition: 2012-03-09
Figured out using REST, see Get report from jasperserver using REST webservice and asp.net C#
By the way, that is also working in Mono! (I develop in Visual Studio, but deploy on Mono)