使用HTTPWebRequest获取SSRS报告:错误401

时间:2012-09-13 07:17:29

标签: asp.net reporting-services httpwebrequest httpwebresponse http-status-code-401

Hello开发人员,

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

我尝试在Google上使用公开图片的网址,它可以100%正常使用。 但我的问题是,当我试图从我的报告服务器获取报告时,我得到A 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()

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

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

提前感谢您的任何帮助:)

1 个答案:

答案 0 :(得分:0)

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

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