我使用這個code來創建和SSRS下載PDF格式的報告,但我收到以下錯誤:如何在運行SQL Server Reporting Service時解決「遠程服務器返回錯誤:(401)未授權」?
The remote server returned an error: (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);
}
}
}
任何幫助將非常感激。
嗨內森,我認爲你是對的;這是一個許可問題。我在想我需要自己設置報表服務器配置。我開發了代碼[這裏](http://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.ireportservercredentials(v = vs80).aspx),但它仍然給我_ 「請求失敗,HTTP狀態401:未經授權」_錯誤。任何建議?謝謝。 – Fred