使用REST webservice和asp.net C#从jasperserver获取报告

tor*_*nfo 11 asp.net rest jasperserver c#-2.0

您可以使用jasperservers webservices(SOAP和REST可用)从Web应用程序获取管理和运行报告.SOAP wsdl与asp.net c#不兼容(至少,我不能让它工作),所以我决定使用REST webservice.

我几乎在那里,但我无法检索报告本身.有谁知道出了什么问题?我在Linux上使用jasperserver CE 4.5.

// Setup WebClient 
WebClient httpclient = new WebClient();

//Basic Auth
httpclient.Credentials = new NetworkCredential("NAME", "PASSWD");
httpclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

// Build resourceDescriptor
string requestXml;
requestXml =  "<resourceDescriptor name="budget_overzicht_per_klant" wsType="reportUnit" uriString="/Declaraties/12change/Klant/budget_overzicht_per_klant"n";
requestXml += " isNew="false">n";
requestXml += "   <label>null</label>n";
requestXml += "   <parameter name="klantid">14</parameter>n";
requestXml += "   <parameter name="start">20120101</parameter>n";
requestXml += "   <parameter name="eind">20120302'</parameter>n";
requestXml += "   <parameter name="Titel">Test 123</parameter>n";
requestXml += "</resourceDescriptor>n";

// Send PUT
string requestAllResult = httpclient.UploadString("http://website/jasperserver/rest/report/Declaraties/12change/Klant/budget_overzicht_per_klant?RUN_OUTPUT_FORMAT=PDF", "PUT", requestXml);

// requestAllResult contains:
//<report>
//  <uuid>f521fe7d-7432-4c47-962c-9fec29bdaa43</uuid>
//  <originalUri>/Declaraties/12change/Klant/budget_overzicht_per_klant</originalUri>
//  <totalPages>4</totalPages>
//  <startPage>1</startPage>
//  <endPage>4</endPage>
//  <file type="application/pdf"><![CDATA[report]]></file>
//</report>
// You have to use the uuid to GET the file 'report'
//
// Extract uuid, filename is always report
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(requestAllResult);
XmlNode node = doc.DocumentElement.SelectSingleNode("uuid");
string uuid = node.InnerText;

//Build GET URL
string reportUrl = "http://website/jasperserver/rest/report/";
reportUrl += uuid;
reportUrl += "?file=report";

// the value of report Url is now 
// "http://website/jasperserver/rest/report/f521fe7d-7432-4c47-962c-9fec29bdaa43?file=report"

// Get report
string report;
report = httpclient.DownloadString(reportUrl);

// Exception, HTTP 404 ERROR????
Run Code Online (Sandbox Code Playgroud)

该错误似乎意味着uuid不在当前会话中.有人这个有用吗?谢谢!

tor*_*nfo 13

我会回答我自己的问题:

PUT在响应头中返回一个Cookie,您必须在后续GET中使用它:

// Send PUT 
string requestAllResult = httpclient.UploadString("http://website/jasperserver/rest/report/Declaraties/12change/Klant/budget_overzicht_per_klant?RUN_OUTPUT_FORMAT=PDF", "PUT", requestXml);

// Get session cookie
string session = httpclient.ResponseHeaders.Get("Set-Cookie");
// Set session cookie
httpclient.Headers.Add("Cookie", session);

// Get report
report=httpclient.DownloadString("http://website/jasperserver/rest/report/f521fe7d-7432-4c47-962c-9fec29bdaa43?file=report");
Run Code Online (Sandbox Code Playgroud)

就是这样!