[英]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。 下面是一個例子:
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.