Downloading a PDF File from a URL

Posted by Vuyiswamb under ASP.NET on 4/16/2015 | Points: 10 | Views : 725 | Status : [Member] [MVP] [Administrator] | Replies : 1
Good Day All

i have a Reporting Service URL that i dynamically pass the Parameters on a Querystrng and if i take that URL and post it on a BRowser it downloads the PDF file. so i want to do this in C# , this code runs in a WCF service

public void GetAccountEmailStatement(string AccountNumber, string EmailAddress)

string remoteUri = "http://myserver/ReportServer/Pages/ReportViewer.aspx?%2fE-Billing%2fISU_PDF_GEN_1&rs:Command=Render&Contract_Account_Number=" + AccountNumber + "&Post_Date=26/02/15&rs:Format=PDF";
string fileName = HttpContext.Current.Server.MapPath("/Temp/" + AccountNumber + ".pdf");
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteUri, fileName); //Error hapeens on this line

catch (SqlException ex)
throw ex;

The Error i get when its supposed to download the file is

[COLOR="#FF0000"]The remote server returned an error: (401) Unauthorized.[/COLOR]

Please help

Thank you for posting at Dotnetfunda


Posted by: Jitendrasoft09 on: 4/19/2015 [Member] [MVP] Starter | Points: 25


It could be the issue of user authorization who does not have the permission to download as we can say 401 error.

I would suggest to check with below details-

* Use DownloadFileAsync method.
* Define code as below-
string myStringWebResource = null;
myStringWebResource = remoteUri + fileName;
* Add authorization as below
<security mode="TransportCredentialOnly">

Please refer below link for more details-

Jitendra Kumar
If my post helps you, plz mark as an answer.

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response