Share via


Receive PDF from WebService and Download it

Question

Thursday, April 2, 2015 11:49 AM

Hello, 

I'm trying to integrate Europass Webservice with the website that I'm currently developing.

This WebService allows you to, through a XML or JSON file, obtain either a PDF or a Word CV.

They Provide the information to communicate with their WS and a Sample XML (here), but you can do your own, since you respect their schema.

To ensure that the xml is okay use the remote upload service and see the resulting CV from your XML in their website.

So my problem is that I can comunicate with the Web service and I also Get a OK Response (Great!), However when downloading the PDF it is all blank.

What am I Missing here?Can you guys please guide me?

To reproduce this, copy the code below and add the content of this http://goo.gl/XyM7Ma to the file in the xmlcontent path.

Thanks in Advance,

Nelssen

CODE:

            
            //POST
            string xmlContent = File.ReadAllText("D:\XML\CV.xml");
            string URL = "https://europass.cedefop.europa.eu/rest/v1/document/to/pdf-cv";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/xml";
            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.UTF8))
            {
                requestWriter.Write(xmlContent);
            }

            //RESPONSE
            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            byte[] byteResponse = Encoding.UTF8.GetBytes (response);
                            Response.Clear();
                            Response.Buffer = true;
                            Response.ContentType = "application/pdf";
                            Response.AddHeader("content-disposition", "attachment;filename=test.pdf"); // Save file         
                            Response.AddHeader("Content-Length", byteResponse.Length.ToString());
                            Response.Charset = "";
                            Response.Cache.SetCacheability(HttpCacheability.NoCache);
                            Response.BinaryWrite(byteResponse);
                            Response.End();
                        }
                    }
                }

All replies (1)

Thursday, April 2, 2015 2:12 PM âś…Answered

StreamReader is a textual reader.  If you are receiving a binary file from the URL then you should be using a BinaryReader.

Michael Taylor
http://blogs.msmvps.com/p3net