【问题标题】:HTTP Authentication and SQL Server Reporting ServicesHTTP 身份验证和 SQL Server 报告服务
【发布时间】:2020-12-13 06:09:12
【问题描述】:

我正在尝试使用 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;

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

【问题讨论】:

  • 你试过了吗,HttpClient client = new HttpClient() { UseDefaultCredentials = true };?或许这篇相关文章会对你有所帮助:(stackoverflow.com/questions/12212116/…)
  • 浏览器默认的http headers与c#不同。您需要使用像 wireshark 或 fiddler 这样的嗅探器,并比较第一个请求中的标头在工作和非工作之间。然后使 c# 标头看起来像工作应用程序。你使用的是什么浏览器? c#中的标头是默认浏览器的UserAgent标头,这可能是失败的原因。

标签: c# sql-server http .net-core reporting-services


【解决方案1】:

您不需要使用基本身份验证来访问该 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 网络服务下载 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_Warning____System_String____

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

【讨论】:

  • 谢谢!使用 WebClient 有效。我也会检查一下使用网络服务的方法。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多