Download SSRS Report and save in specific location (C#) (Unauthorized)
<p><strong>Question</strong></p>

<p>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. </p>

<p><strong>The Issue</strong></p>

<p>However I am constantly getting the below error <strong>(401) Unauthorized</strong>, no matter which credentials I enter:</p>

<blockquote>
  <p>An exception of type 'System.Net.WebException' occurred in System.dll
  but was not handled in user code</p>
  
  <p>Additional information: The remote server returned an error: (401)
  Unauthorized.</p>
</blockquote>

<p>I am using this code (C#): </p>

<pre><code>WebClient myWebClient = new WebClient();
NetworkCredential netCredential = new NetworkCredential("username", "password");
myWebClient.Credentials = netCredential;

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

byte[] myDataBuffer = myWebClient.DownloadData(theURL);
</code></pre>

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

<p>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.</p>

<p>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: "<a href="http://www.axmag.com/download/pdfurl-guide.pdf" rel="noreferrer">http://www.axmag.com/download/pdfurl-guide.pdf</a>"</p>

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