Bhanu Reddy
Bhanu Reddy

Reputation: 215

SSRS PDF report downloaded from code using HTTPCLIENT is corrupted

SSRS Report is hosted on a virtual machine in Azure with windows authentication enabled. I am able to download PDF by hitting the url in the browser by giving credentials but the same is not working with code. I am able to download PDF but which is saying as corrupted. When I hit the url in browser its downloading PDF with size 900KB but the same from code its downloading 120kb and its saying as corrupted. Is this any security issue where less bytes are downloaded?

Below is the sample code:

var handler = new HttpClientHandler { Credentials = new NetworkCredential("username", "Password", "domain") }; HttpClient client = new HttpClient(handler); HttpResponseMessage result = client.PostAsync(requestConnection, content).Result; var responseData = result.Content.ReadAsByteArrayAsync().Result;

Upvotes: 1

Views: 361

Answers (1)

craigster
craigster

Reputation: 124

If you are trying to export a SSRS Report to PDF here is some code I use generate a report into a MemoryStream but you can save it to file if you want to

All variables end "VAR" and are strings

I hope this Helps you

                            Dim ServerUrl As New Uri(ReportServerUrlVAR)
                            Dim ReportViewer1 As New ReportViewer
                            ReportViewer1.ServerReport.ReportServerUrl = ServerUrl
                            ReportViewer1.ServerReport.ReportPath = "/" & ReportsPathNameVAR & "/" & ReportNameVAR
                            ReportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials = New Net.NetworkCredential(ReportServiceUserNameVAR, m_Datamanager.Settings.ReportServicePasswordVAR)

                            ReportViewer1.RefreshReport()

                            Dim mimeType As String = String.Empty
                            Dim encoding As String = String.Empty
                            Dim extension As String = String.Empty
                            Dim deviceInfo As String = String.Empty
                            Dim streamids As String() = Nothing
                            Dim warnings As Warning() = Nothing
                            Dim format As String = "PDF"
                            deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>"
                            Dim bytes As Byte() = ReportViewer1.ServerReport.Render(format, deviceInfo, mimeType, encoding, extension, streamids, warnings)

                            Dim ms As New MemoryStream(bytes)

                            'Create Email
                            Dim SmtpServer As New SmtpClient
                            Dim mail As New MailMessage
                            If EmailUserNameVAR <> String.Empty Then
                                SmtpServer.Credentials = New Net.NetworkCredential(EmailUserNameVAR, EmailPasswordVAR)
                            End If
                            SmtpServer.Port = EmailServerPortVAR
                            SmtpServer.Host = EmailServerVAR
                            mail = New MailMessage()
                            mail.From = New MailAddress(EmailFromAddressVAR)
                            mail.To.Add(m_EmailAddress)
                            Dim NewAttach As New Attachment(ms, ReportNameVAR & ".pdf")
                            mail.Attachments.Add(NewAttach)
                            mail.Subject = "Automatic Email Report: " & ReportNameVAR
                            mail.Body = "This message is an Automatic Email Report: " & ReportNameVAR & vbCrLf & "Please Check the Attached File."
                            SmtpServer.Send(mail)
                            Application.ExitThread()

Upvotes: 1

Related Questions