Downloading a PDF File from a URL
-
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) { try { // 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; } finally { this.con.Close(); } } The Error i get when its supposed to download the file is The remote server returned an error: (401) Unauthorized. Please help
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
-
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) { try { // 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; } finally { this.con.Close(); } } The Error i get when its supposed to download the file is The remote server returned an error: (401) Unauthorized. Please help
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
The error message is very clear and explicit: "The remote server returned an error: (401) Unauthorized." That means that you need to log your downloader in to the site in some way in order to get access to teh files. I'd suggest you talk to the site owners about the best way to do that!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...