2016/12/30

Asp.net MVC call Jasper report via REST v2 service

Author: Willie Chou
Date: 2016/12/30


Directly call jasper report via http request.
Please see below scripts.
Hope this is help for you ! Happy New Year !

Please note! the variable _ReportURL format
  _ReportURL  = http://<web url>/jasperserver/rest_v2/reports

  public  ActionResult PrintPO(string POEntry)
        {

           
            //_ReportURL = System.Web.Configuration.WebConfigurationManager.AppSettings["ReportURL"];

            //_ReportUser = System.Web.Configuration.WebConfigurationManager.AppSettings["ReportUser"];

            //_ReportUserPWD = System.Web.Configuration.WebConfigurationManager.AppSettings["ReportUserPWD"];


            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(_ReportUser + ":" + _ReportUserPWD));

            string URL = _ReportURL + "/report.pdf?po=" + POEntry;



            CookieContainer myContainer = new CookieContainer();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

          

            request.CookieContainer = myContainer;

            request.PreAuthenticate = true;                       

            request.Headers.Add("Authorization", "Basic " + encoded);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();



            var fileBytes = response.GetResponseStream();

            string filename = String.Format("PO{0}_{1}.pdf", POEntry, DateTime.UtcNow.ToString("yyyMMddHHmmssfff"));

            return File(fileBytes, response.ContentType,filename); //preview in browser please remove filename

        }



Result as below pix.

沒有留言: