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

时间:2012-03-08 19:34:39

标签: asp.net rest c#-2.0 jasperserver

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

我几乎在那里,但我无法检索报告本身。有谁知道出了什么问题? 我在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????

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

2 个答案:

答案 0 :(得分: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");

就是这样!

答案 1 :(得分:1)

老兄非常感谢这是我通过REST从JasperServer获取报告时发现的最有用的帖子,我正在使用Ruby on Rails。升级到JS 5.0之后,我发现SOAP接口非常不可靠,基本上你必须点击它两次才能生成报告。我没有成功使用rest_v2接口,它似乎永远不会遵守传递给它的任何参数。

所以我尝试了旧的REST界面,并且工作正常。对于任何Rails人来说,这是我的(稍微凌乱)控制台代码。

report = 'TempSpeedLimit'
params = {:tsl_id => 744}
request_body = "<resourceDescriptor name='#{report}' wsType='reportUnit' uriString='/reports/hawk/#{report}' isNew='false'><label>null</label>"
params.each{|k,v| request_body += "<parameter name='#{k}'>#{v}</parameter>"}
request_body += "</resourceDescriptor>"
body_put = ''
body_get = ''
cookie_put = ''

uri_put = URI.parse("http://<yourhost>:8080/jasperserver/rest/report/reports/hawk/#{report}")
http_put = Net::HTTP.new(uri_put.host, uri_put.port)
http_put.start do |http|
req = Net::HTTP::Put.new(uri_put.path + "?RUN_OUTPUT_FORMAT=PDF")
req.basic_auth('jasperadmin', 'secretpassword')
req.body = request_body
resp = http.request(req)
body_put = resp.body
cookie_put = resp['Set-Cookie']
end

xml = REXML::Document.new(body_put)
uuid = xml.elements["report/uuid"].text

uri_get = URI.parse("http://<yourhost>:8080/jasperserver/rest/report/#{uuid}")
http_get = Net::HTTP.new(uri_get.host, uri_get.port)
http_get.start do |http|
req = Net::HTTP::Get.new(uri_get.path + "?file=report")
req.basic_auth('jasperadmin', 'secretpassword')
req['cookie'] = cookie_put
resp = http.request(req)
body_get = resp.body
end

f = File.new('test.pdf', 'wb')
f.write(body_get)
f.close