1

I am trying to download pdf from my jasperserver

here is the demo code :

   public void Genrate_Report_By_ID(String report_id )
    {

    String Server_Adress = "http://localhost:8081/jasperserver-pro";
        try
        {

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

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


            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Server_Adress+"/rest/resource/organizations/M/Report_By_ID");
            request.Method = "GET";
            string requestXml = String.Empty;
            request.Credentials = httpclient.Credentials;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                requestXml = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
            }


            // Send PUT
            string requestAllResult = "";
            requestAllResult = httpclient.UploadString(Server_Adress + "/rest/report/organizations/M/Report_By_ID?RUN_OUTPUT_FORMAT=PDF", "PUT", requestXml);

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

            // Extract uuid, filename is always report
            XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(requestAllResult);
            XmlNode node = doc.DocumentElement.SelectSingleNode("uuid");
            string uuid = node.InnerText;

            //Build GET URL
            string reportUrl = Server_Adress + "/rest/report/";
            reportUrl += uuid;
            reportUrl += "?file=report";


         }
        catch (Exception ex)
        {

            throw ex;
        }


    }

But this Exception occurs .

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Credential are correct .

  • Did you read http://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-connection-an-existing-connection-was-f ? – ADyson Dec 12 '16 at 12:51
  • @ADyson yes i did but its no help . server is running fine . i can generate same report there . but when i try with c# i get this error – Muhammad Junaid Khan Dec 12 '16 at 13:07
  • which request is causing the error, the upload or the download? – ADyson Dec 12 '16 at 13:30
  • The upload . in above code . – Muhammad Junaid Khan Dec 12 '16 at 13:32
  • what is Server_Address? It's not declared in your example. Have you verified that the URL and XML being generated is correct? – ADyson Dec 12 '16 at 13:34
  • `String Server_Adress = "http://localhost:8081/jasperserver-pro";` – Muhammad Junaid Khan Dec 12 '16 at 13:38
  • can you use another tool like PostMan to call the URL, and verify it it works? Then we can see if the C# is the issue or, or the URL used, or the server. It does sound like the server is closing the connection for some reason, possibly after the initial connection is made. Sometimes this can be caused by firewalls, proxies or anti-virus software interfering with network connections (even though you're accessing localhost, these things can still be applicable). – ADyson Dec 12 '16 at 13:45
  • i will update upper code – Muhammad Junaid Khan Dec 12 '16 at 14:05

0 Answers0