Download SSRS Report and save in specific location (C#) (Unauthorized)

Crezzer7 picture Crezzer7 · Oct 31, 2016 · Viewed 11.6k times · Source

Question

I am attempting to download an SSRS report via its URL into a data buffer (byte array), so therefore I can then save it in a particular folder, with a name of my choosing. I am open to suggestions of different ways of doing this if required.

The Issue

However I am constantly getting the below error (401) Unauthorized, no matter which credentials I enter:

An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

Additional information: The remote server returned an error: (401) Unauthorized.

I am using this code (C#):

WebClient myWebClient = new WebClient();
NetworkCredential netCredential = new NetworkCredential("username", "password");
myWebClient.Credentials = netCredential;

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

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

example URL (this downloads the file perfectly if I simply copy the below URL into my browser, no issues with the access whatsoever): http://ReportServer/ReportServer_MYSERVER/Pages/ReportViewer.aspx?%2fPurchaseOrder&rs:Command=Render&OrderID=100&rs:ClearSession=true&rs:Format=PDF

My machine is on a domain, and I have tried these credentials, along with the domain, and also the database SA account, but both deliver the same issue.

I know this code works when downloading a PDF file, as I hard-coded a random URL from the internet and this worked, for example: "http://www.axmag.com/download/pdfurl-guide.pdf"

Thankyou all for help on this matter, im hoping that this is possible as this would be a very handy addition to my application

Answer

Crezzer7 picture Crezzer7 · Oct 31, 2016

it turns out it was as simple as this for my scenario (using Default Credentials):

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

this code above enables any SSRS report to be downloaded as a byte array. This then means that it can be saved in a specified location, with a name of your choice:

var filename = "Test.PDF";
var fileStructureLocal = "C:\\Test";
var fileStructureNetwork = "\\\\NetworkDrive\TestFolder";

var fileLocation = fileStructureNetwork + "\\" + filename;

 if (System.IO.File.Exists(url) == true)
        {
            //DO NOTHING
        }
 else
        {
            System.IO.File.WriteAllBytes(url, myDataBuffer);
            //SAVE FILE HERE
        }

this next block is how you rename the file, and specify the location. I have also added a check to see if it already exists, if it does it simply does nothing.

I hope this helps as this is something I have been trying to get working for a long time!