我正在使用此代码从 SSRS 创建和下载 PDF 报告,但收到以下错误:
远程服务器返回错误:(401) Unauthorized。
我的代码是:
protected void Page_Load(object sender, EventArgs e)
{
MemoryStream ms;
// this name is what the user will see when they are prompted for download.
string customFileName = "NewFileName.pdf";
if (!this.IsPostBack)
{
try
{
string strRequest = "http://myServer.com/ReportServer?%2fmetadata_report%2fMetadataReport1&rs%3aCommand=Render&rs%3AFormat=PDF&grpId=3";
WebRequest request = WebRequest.Create(strRequest);
request.ContentType = "application/pdf";
string userName = "username";
string password = "password";
string domain = "myServer.com";
// Create and then pass in network credentials to acecss the reporting server
System.Net.NetworkCredential credentials = new NetworkCredential(userName, password, domain);
request.Credentials = credentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (response)
{
// Get the stream from the server
using (Stream stream = response.GetResponseStream())
{
// Use the ReadFully method from the link above:
byte[] data = ReadFully(stream, response.ContentLength);
// Return the memory stream.
ms = new MemoryStream(data);
}
}
// Clear out the headers that may already exist, then set content type and add a header to name the file
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; ");
//// Write the memory stream containing the pdf file directly to the Response object that gets sent to the client
ms.WriteTo(Response.OutputStream);
}
}
}
任何帮助将非常感激。