Hello fellow developers,
I currently have a task to make a Bulk Report Exporter. My thinking is that I have to create a loop for each set of parameters, and pull the report with an HTTPWebRequest and Response, and save each to a file.
I have tried using a URL of a publicly available image on Google, and it works 100%. But My problem is, when I try to get the report from my report server, I get A 401 Unauthorized exception.
Here is My Code:
Dim ReportServerURL As String = System.Configuration.ConfigurationManager.AppSettings.Get("reportServerURL")
'Get the needed credentials for opening a connection to the report server.
Dim ReportServerUN As String = System.Configuration.ConfigurationManager.AppSettings.Get("reportServerUN")
Dim ReportServerPW As String = System.Configuration.ConfigurationManager.AppSettings.Get("reportServerPW")
Dim ReportServerDomain As String = System.Configuration.ConfigurationManager.AppSettings.Get("reportServerDomain")
'Get the path to where the reports must be saved.
Dim BTIReportFolderPath As String = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings.Get("bulkReportingFilePath"))
'Create a http request or connecting to the report server.
Dim ReportHTTPRequest As HttpWebRequest = Nothing
'Create a http response to catch the data stream returned from the http request.
Dim ReportHTTPResponse As HttpWebResponse = Nothing
'Create a stream to read the binary data from the http reponse.
Dim ReportStream As Stream = Nothing
Dim ReportFileStream As FileStream = Nothing
'Create an array of bytes to get the binary data from the stream.
Dim ReportBytes As Byte()
Dim ReportBuffer As Integer = 1024
Dim ReportBytesRead As Integer = 0
'Create a webrequest to get the report with all the report parameters included.
ReportHTTPRequest = WebRequest.Create(ReportServerURL & "&UserID=" & UserID & "&InstrumentID=" & InstrumentID & "&AssessmentID=" & AssessmentID & "&OverViewChartURL=" & reportGraphURL & BTIGraphGenerator.SaveBTIGraph(FactorGraph, reportGraphImagePath) & "&NeuroticismChartURL=" & reportGraphURL & BTIGraphGenerator.SaveBTIGraph(NeuroticismGraph, reportGraphImagePath) & "&ExtraversionChartURL=" & reportGraphURL & BTIGraphGenerator.SaveBTIGraph(ExtraversionGraph, reportGraphImagePath) & "&ConscientiousnessChartURL=" & reportGraphURL & BTIGraphGenerator.SaveBTIGraph(ConscientiousnessGraph, reportGraphImagePath) & "&ExperienceChartURL=" & reportGraphURL & BTIGraphGenerator.SaveBTIGraph(ExperienceGraph, reportGraphImagePath) & "&AgreeablenessChartURL=" & reportGraphURL & BTIGraphGenerator.SaveBTIGraph(AgreeablenessGraph, reportGraphImagePath))
'Dim ReportServerCredentials As New CredentialCache()
'ReportServerCredentials.Add(New Uri(ReportServerURL), "Basic", New NetworkCredential(ReportServerUN, ReportServerPW))
'ReportHTTPRequest.PreAuthenticate = True
'ReportHTTPRequest.Credentials = New NetworkCredential(ReportServerUN, ReportServerPW)
'Get the response from the request.
Dim credentials As String = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(ReportServerUN & ":" & ReportServerPW))
ReportHTTPRequest.Headers.Add("Authorization", "Basic" & credentials)
'ReportHTTPRequest.Credentials = New NetworkCredential(ReportServerUN, ReportServerPW, ReportServerDomain)
'Get the response from the request.
Try
ReportHTTPResponse = ReportHTTPRequest.GetResponse()
Catch ex As Exception
ReportHTTPResponse = ReportHTTPRequest.GetResponse()
End Try
'Read the binary stream from the http response.
ReportStream = ReportHTTPResponse.GetResponseStream()
ReportBytes = New Byte(ReportBuffer) {}
ReportBytesRead = ReportStream.Read(ReportBytes, 0, ReportBuffer)
ReportFileStream = New FileStream(NewBTIReportFolder.FullName & "ASiame" & Guid.NewGuid.ToString() & ".pdf", FileMode.Create)
Do While ReportStream.CanRead And ReportBytesRead > 0
ReportFileStream.Write(ReportBytes, 0, ReportBytesRead)
ReportBytesRead = ReportStream.Read(ReportBytes, 0, ReportBuffer)
Loop
ReportHTTPResponse.Close()
ReportStream.Close()
ReportFileStream.Close()
All the bits commented out is all the methods I have tried to pass credentials.
I am an administrator on the server, but still I cannot get authenticated.
Thanks in advance for any help :)