I am trying to migrate a very old SQL Server environment (2005!) to 2019. In our old environment we have an SSIS package that includes a VB script task to generate PDF copies of a number of reports and save them to a specified location. When I try and re-create this package using Visual Studio 2019 with the Integration Services add-in, and run this against our SSRS 2019 instance, all the PDFs get generated with 0 bytes. I have a suspicion that this is related to authentication in reporting services 2019 - whenever I run the reports manually, I am prompted for credentials, which doesn't happen on my 2005 environment.
The Main() procedure that does the work is:
Public Sub Main()
Dim url, destination As String
destination = Dts.Variables("FolderDestination").Value.ToString + "\" + Dts.Variables("SchemeMemberID").Value.ToString + "_" + Format(Now, "yyyyMMdd_HHmmss") + ".pdf"
url = "http://my-report-server.my.org/ReportServer/Pages/ReportViewer.aspx?/ReportFolder/ReportName&rs:Command=Render&sKey0=" + Dts.Variables("Param0").Value.ToString + "&sKey1=" + Dts.Variables("Param1").Value.ToString + "&rs:Format=PDF"
SaveFile(url, Dts.Variables("svcAccountName").Value.ToString, Dts.Variables("svcAccountPwd").Value.ToString, destination)
Dts.TaskResult = ScriptResults.Success
End Sub
This is calling the following procedure to generate and save the PDF files. The commented line is what it was doing before @billinkc posted a response - I made the suggested changes, but still get 0 byte files:
Protected Sub SaveFile(ByVal url As String, ByVal svcAccountName As String, ByVal svcAccountPwd As String, ByVal localpath As String)
Dim loRequest As System.Net.HttpWebRequest
Dim loResponse As System.Net.HttpWebResponse
Dim loResponseStream As System.IO.Stream
Dim loFileStream As New System.IO.FileStream(localpath, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim laBytes(256) As Byte
Dim liCount As Integer = 1
Dim Cred As New System.Net.NetworkCredential(svcAccountName, svcAccountPwd)
Dim CredCache As New System.Net.CredentialCache
Try
CredCache.Add(New Uri(url), "Basic", Cred)
loRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
'loRequest.Credentials = System.Net.CredentialCache.DefaultCredentials
loRequest.Credentials = Cred
loRequest.Timeout = 600000
loRequest.Method = "GET"
loResponse = CType(loRequest.GetResponse, System.Net.HttpWebResponse)
loResponseStream = loResponse.GetResponseStream
Do While liCount > 0
liCount = loResponseStream.Read(laBytes, 0, 256)
loFileStream.Write(laBytes, 0, liCount)
Loop
loFileStream.Flush()
loFileStream.Close()
Catch ex As Exception
End Try
End Sub
After adding a line in the Catch to surface the Exception, the script reports
The remote server returned an error: (401) Unauthorized