[英]Using HTTPWebRequest to get SSRS report: Error 401
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.
我的想法是,我必须为每个参数集创建一个循环,并使用HTTPWebRequest和Response提取报告,然后将每个参数保存到文件中。
I have tried using a URL of a publicly available image on Google, and it works 100%. 我尝试使用Google上公开图像的URL,它可以100%正常工作。 But My problem is, when I try to get the report from my report server, I get A 401 Unauthorized exception.
但是我的问题是,当我尝试从报表服务器获取报表时,出现A 401未经授权的异常。
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 :) 在此先感谢您的帮助:)
I have found the answer. 我找到了答案。 All the time I have tried: ReportHTTPRequest.Credentials = CredentailCache.DefaultCredentials() Instead of: ReportHTTPRequest.Credentials = CredentailCache.DefaultNetworkCredentials()
我一直尝试:ReportHTTPRequest.Credentials = CredentailCache.DefaultCredentials()而不是:ReportHTTPRequest.Credentials = CredentailCache.DefaultNetworkCredentials()
On the second problem, I found using the local IP instead of the URL works perfectly. 关于第二个问题,我发现使用本地IP代替URL可以很好地工作。 I guess using the URL tries to route the request externally from the server, and then back to the server.
我猜想使用URL尝试将请求从服务器外部路由,然后再返回到服务器。 Using the local IP kept the request internal and magically worked.
使用本地IP将请求保持在内部,并且可以神奇地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.