C# HTTP身份验证和SQL Server Reporting Services

C# HTTP身份验证和SQL Server Reporting Services,c#,sql-server,http,.net-core,reporting-services,C#,Sql Server,Http,.net Core,Reporting Services,我正在尝试使用C#(一个.Net核心控制台应用程序)将SQL Server Reporting Services报表导出为PDF格式。如果我将以下内容直接粘贴到浏览器URL栏中,PDF将成功保存到我的“下载”文件夹中: https://MyServer:443/ReportServer?/MyReport&rs:Format=PDF&ReportParameter=Whatever 下面是我用来尝试在C#中做同样事情的代码: 结果是“未经授权”。我缺少什么?您不需要使用基本身份

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

https://MyServer:443/ReportServer?/MyReport&rs:Format=PDF&ReportParameter=Whatever
下面是我用来尝试在C#中做同样事情的代码:


结果是“未经授权”。我缺少什么?

您不需要使用基本身份验证来访问该URL,而应该使用SSRS Web服务的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);
您还可以使用ReportExecution2005Web服务下载PDF。以下是一个例子:

_

  • 在项目-->添加服务引用->高级->添加Web引用->URL下:http:////reportexecution2005.asmx 并将其命名为ReportExecution2005
  • 使用以下代码创建一个方法
  • ReportExecutionService rsExec=newreportexecutionservice();
    rsExec.Credentials=System.Net.CredentialCache.DefaultCredentials;
    rsExec.Url=”http:////reportexecution2005.asmx";
    字符串historyID=null;
    字符串reportPath=“/”;
    LoadReport(reportPath,historyID);
    GetPropertiesSample.ReportExecution2005.ParameterValue[]executionParams;
    executionParams=new GetPropertiesSample.ReportExecution2005.ParameterValue[1];
    executionParams[0]=新的GetPropertiesSample.ReportExecution2005.ParameterValue();
    executionParams[0]。Name=“我的参数名”;
    executionParams[0].Value=“我的参数值”;
    新的GetPropertiesSample.ReportExecution2005.ParameterValue();
    rsExec.SetExecutionParameters(executionParams,“en-us”);
    字符串deviceInfo=null;
    字符串扩展;
    字符串编码;
    字符串模拟类型;
    GetPropertiesSample.ReportExecution2005.Warning[]warnings=null;
    字符串[]streamIDs=null;
    string format=“PDF”;
    Byte[]results=rsExec.Render(格式、设备信息、输出扩展名、输出mimeType、输出编码、输出警告、输出流ID);
    FileStream stream=File.OpenWrite(“c:\\My Destination Folder Name>\\\.pdf”);
    stream.Write(results,0,results.Length);
    stream.Close();
    
    您是否尝试过,
    HttpClient=new-HttpClient(){UseDefaultCredentials=true}?也许这篇相关的文章可以帮助您:()浏览器中的默认http头与c#不同。您需要使用诸如wireshark或fiddler之类的嗅探器,并在第一个请求中比较工作和非工作的头。然后使c#头看起来像工作应用程序。你用的是什么浏览器?c#中的标题是默认浏览器是UserAgent标题,这可能是失败的原因。谢谢!使用WebClient成功了。我还将使用web服务检查该方法。
    
    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);
    
    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();