0

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 :)

simonc
  • 41,632
  • 12
  • 85
  • 103
Eugene de Lange
  • 93
  • 2
  • 12
  • I have found the answer. All the time I have tried: ReportHTTPRequest.Credentials = CredentailCache.DefaultCredentials() Instead of: ReportHTTPRequest.Credentials = CredentailCache.DefaultNetworkCredentials() – Eugene de Lange Sep 13 '12 at 08:56
  • Good to hear! You should really convert your comment into an answer, and accept it (after 24 hrs) so others can easily see how this issue was resolved. – Jeroen Sep 14 '12 at 10:52
  • Alright, now the problem originally existed on our dev server, but Friday we uploaded to the clients QA server, and the ran into the same problem, however this time we can find a solution to it. Does anyone have any recommendations? – Eugene de Lange Sep 25 '12 at 06:27

1 Answers1

0

I have found the answer. All the time I have tried: ReportHTTPRequest.Credentials = CredentailCache.DefaultCredentials() Instead of: ReportHTTPRequest.Credentials = CredentailCache.DefaultNetworkCredentials()

On the second problem, I found using the local IP instead of the URL works perfectly. I guess using the URL tries to route the request externally from the server, and then back to the server. Using the local IP kept the request internal and magically worked.

Eugene de Lange
  • 93
  • 2
  • 12