Reporting Services as a PDF via WebRequest in C # 3.5 "File Type Not Supported"

I have inherited an outdated application that is supposed to take on-the-fly pdf from a report services server. Everything works fine until you try to open the returned pdf file and adobe acrobat tells you:

Adobe Reader could not open 'thisStoopidReport'.pdf' because it is either an unsupported file type or because the file is damaged (for example, it was sent as an email attachment and is not decoded correctly).

I made some initial troubleshooting problems. If I replaced the url in the WebRequest.Create () call with a valid pdf file on my local computer, that is: @ "C: temp / validpdf.pdf"), then I get a valid PDF.

The report itself seems to be working fine. If I manually print the URL of the report report service that should generate the PDF file, I will be prompted to authenticate the user. But after delivery I get a valid pdf file.

I replaced the actual URL, username, userpass and domain strings in the code below with dummy values ​​for obvious reasons.

        WebRequest request = WebRequest.Create(@"http://x.x.x.x/reportServer?/reports/reportNam&rs:format=pdf&rs:command=render&rc:parameters=blahblahblah");
        int totalSize = 0;
        request.Credentials = new NetworkCredential("validUser", "validPass", "validDomain");
        request.Timeout = 360000; // 6 minutes in milliseconds.
        request.Method = WebRequestMethods.Http.Post;
        request.ContentLength = 0;
        WebResponse response = request.GetResponse();
        Response.Clear();
        BinaryReader reader = new BinaryReader(response.GetResponseStream());
        Byte[] buffer = new byte[2048];
        int count = reader.Read(buffer, 0, 2048);
        while (count > 0)
        {
            totalSize += count;
            Response.OutputStream.Write(buffer, 0, count);
            count = reader.Read(buffer, 0, 2048);
        }
        Response.ContentType = "application/pdf";
        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.CacheControl = "private";
        Response.Expires = 30;
        Response.AddHeader("Content-Disposition", "attachment; filename=thisStoopidReport.pdf");
        Response.AddHeader("Content-Length", totalSize.ToString());
        reader.Close();
        Response.Flush();
        Response.End();
+5
source share
3 answers

pdf, notepad.exe. , HTML. -, , PDF. - HTML, PDF.

- PDF , http://www.somesite.com/file.pdf, . , , , -, pdf.

, , ISAPI DLL, , , pdf . ISAPI DLL PDF "application/pdf".

+1

, , HTML, URL-, . , , PUT GET.

, :

request.Method = WebRequestMethods.Http.Post;

... err pdf... .

, . .

+1

Could your problem be caused by declaring your byte array of length 2048 instead of basing the length on the length of the stream returned by GetResponseStream ()?

0
source

All Articles