I am creating a report from SSRS from C# using WebClient . But when i try to download the file this gives me exception the remote server returned an error (401) unauthorized
. I have google it but not get any result to resolve this.
URl i take reference Stack overflow Question and second link Stack overflow Question 2 for my reference . Can you please help me to resolve this
private void OpenSSRSLabel(int ordId) {
try {
string ReportURL = "http://MySERVER/ReportServer/Pages/ReportViewer.aspx?%2fSystem%2fWebOrderEntry_Label&ORDERNUM=";
if (_orderDAC == null)
_orderDAC = new OrderDAC();
DataTable _dt = _orderDAC.GetOderByID(ordId);
string FileURL = ReportURL + _dt.Rows[0]["ORDERNUM"].ToString() + "&rs:Command=Render&rs:Format=PDF";
string filename = string.Format("OrderLabel{0}.pdf", DateTime.Now.ToString("dd-H-mmss"));
string file = Server.MapPath("~/Temp/" + filename);
if (System.IO.File.Exists(file)) {
File.Delete(file);
}
// Implement in Live Server
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("MYSERVER\\Admin", "C47");
client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
client.DownloadFile(FileURL, file); // Exception
string path = Page.ResolveClientUrl("~/Temp/" + filename);
ScriptManager.RegisterStartupScript(Page, typeof(System.Web.UI.Page), "OpenWindow", "window.open('" + path + "');", true);
}
catch (Exception ex) {
}
}