0

各位开发者您好,

我目前的任务是制作批量报告导出器。我的想法是我必须为每组参数创建一个循环,并使用 HTTPWebRequest 和 Response 拉取报告,并将每个参数保存到文件中。

我曾尝试在 Google 上使用公开可用图片的 URL,它可以 100% 工作。但我的问题是,当我尝试从我的报表服务器获取报表时,我得到了 401 Unauthorized 异常。

这是我的代码:

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

注释掉的所有位都是我尝试传递凭据的所有方法。

我是服务器上的管理员,但我仍然无法通过身份验证。

在此先感谢您的帮助:)

4

1 回答 1

0

我找到了答案。我一直在尝试: ReportHTTPRequest.Credentials = CredentailCache.DefaultCredentials() 而不是: ReportHTTPRequest.Credentials = CredentailCache.DefaultNetworkCredentials()

在第二个问题上,我发现使用本地 IP 而不是 URL 效果很好。我猜想使用 URL 会尝试从服务器外部路由请求,然后再返回服务器。使用本地 IP 将请求保持在内部并神奇地工作。

于 2015-05-14T09:16:53.073 回答