0

I'm trying to export a SQL Server Reporting Services report as PDF, using C# (a .Net Core console application). If I paste the following directly into a browser URL bar, the PDF is successfully saved to my 'Downloads' folder:

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

Here's the code I'm using to try to do the same thing in 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;

The result is "Unauthorized". What am I missing?

Brendan Reynolds
  • 991
  • 2
  • 9
  • 19
  • 1
    Have you tried, `HttpClient client = new HttpClient() { UseDefaultCredentials = true };`? Perhaps this related article will help you: (https://stackoverflow.com/questions/12212116/how-to-get-httpclient-to-pass-credentials-along-with-the-request) – Ryan Wilson Aug 24 '20 at 15:24
  • The default http headers in the browser is different from c#. You need to use a sniffer like wireshark or fiddler and compare the headers in first request between working and non working. Then make c# headers looking like working application. What browser are you using? The headers in c# are the default browser is the UserAgent header and this may be the reason for the failure. – jdweng Aug 24 '20 at 15:56

1 Answers1

3

You don't need to use basic authentication to access that URL and instead should use the DefaultCredential of the SSRS WebService

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

You can also download the PDF using the ReportExecution2005 Web Service. Here is an example:

https://learn.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. Under Project --> Add Service Reference -> Advanced -> Add Web Reference -> URL: http:////reportexecution2005.asmx and named it ReportExecution2005.
  2. Create a method with the following code
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();
vvvv4d
  • 3,881
  • 1
  • 14
  • 18