繁体   English   中英

HTTP 身份验证和 SQL Server Reporting Services

[英]HTTP Authentication and SQL Server Reporting Services

我正在尝试使用 C#(.Net Core 控制台应用程序)将 SQL Server Reporting Services 报告导出为 PDF。 如果我将以下内容直接粘贴到浏览器 URL 栏中,PDF 将成功保存到我的“下载”文件夹中:

https://MyServer:443/ReportServer?/MyReport&rs:Format=PDF&ReportParameter=Whatever

这是我用来尝试在 C# 中做同样事情的代码:

HttpClient client = new HttpClient();

var byteArray = Encoding.ASCII.GetBytes("MyUsername:MyPassword");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
    "Basic", Convert.ToBase64String(byteArray));

var result = await client.GetAsync("https://MyServer:443/ReportServer?/MyReport&rs:Format=PDF&ReportParameter=Whatever);

Console.WriteLine(result.StatusCode);

return result;

结果是“未经授权”。 我错过了什么?

您不需要使用基本身份验证来访问该 URL,而是应该使用 SSRS WebService 的 DefaultCredential

var theURL = "http://ReportServer/ReportServer_MYSERVER/Pages/ReportViewer.aspx?%2fPurchaseOrder&rs:Command=Render&OrderID=100&rs:ClearSession=true&rs:Format=PDF";

WebClient Client = new WebClient();
Client.UseDefaultCredentials = true;

byte[] myDataBuffer = Client.DownloadData(theURL);

您还可以使用ReportExecution2005 Web 服务下载 PDF。 下面是一个例子:

https://docs.microsoft.com/en-us/dotnet/api/reportexecution2005.reportexecutionservice.render?redirectedfrom=MSDN&view=sqlserver-2016#ReportExecution2005_ReportExecutionService_Render_System_String_System_String_System_String__System_String__System_String__ReportExecution2005

  1. 在项目--> 添加服务引用-> 高级-> 添加Web 引用-> URL:http:////reportexecution2005.asmx 并将其命名为ReportExecution2005。
  2. 使用以下代码创建一个方法
ReportExecutionService rsExec = new ReportExecutionService();
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Url = "http://<My Server Name>/<My Report Server Name>/reportexecution2005.asmx";

string historyID = null;
string reportPath = "/<My Folder Name>/<My SSRS Report Name>";
rsExec.LoadReport(reportPath, historyID);

GetProperteriesSample.ReportExecution2005.ParameterValue[] executionParams;
executionParams = new GetProperteriesSample.ReportExecution2005.ParameterValue[1];
executionParams[0] = new GetProperteriesSample.ReportExecution2005.ParameterValue();
executionParams[0].Name = "My Parameter Name";
executionParams[0].Value = "My Parameter Value";
new GetProperteriesSample.ReportExecution2005.ParameterValue();

rsExec.SetExecutionParameters(executionParams, "en-us");

string deviceInfo = null;
string extension;
string encoding;
string mimeType;
GetProperteriesSample.ReportExecution2005.Warning[] warnings = null;
string[] streamIDs = null;
string format = "PDF";

Byte[] results = rsExec.Render(format, deviceInfo, out extension, out mimeType, out encoding, out warnings, out streamIDs);
FileStream stream = File.OpenWrite("c:\\My Destination Folder Name>\\<My PDF Report Name>.pdf");
stream.Write(results, 0, results.Length);
stream.Close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM