我似乎无法使用 HttpClient 向报告服务器发送请求,因为结果总是 401(未经授权)。
Action 是
public async Task<FileStreamResult> SSRSTest()
{
//set credentials
using (var handler = new HttpClientHandler {
Credentials = new NetworkCredential("userName", "password"),
UseDefaultCredentials = false
})
using (var httpClient = new HttpClient(handler))
{
//get *.pdf from report server
var response = await httpClient .GetStreamAsync("http://someip/ReportServer/Pages/ReportViewer.aspx?TheRemainingPartOfURL");
var contentDisposition = new ContentDisposition
{
FileName = "SomeReport.pdf",
Inline = false
};
//set content disposition
Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
//return the file
return File(response, "application/pdf");
}
}
附加:
Pass a Report Parameter Within a URL
最佳答案
我已经使用 Fiddler 查看当我使用浏览器登录时发生了什么
Auth 标签是
WWW-Authenticate Header is present: Negotiate
WWW-Authenticate Header is present: NTLM
所以即使有人告诉我身份验证是基本的,我也需要使用以下内容
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri("http://youruri"),"NTLM",new NetworkCredential(username, password));
using (var handler = new HttpClientHandler
{
Credentials = credentialCache
})
HttpClient 的其余代码是相同的。
附加:
Authentication with the Report Server
关于c# - 在 ASP.NET MVC Action 中使用 HttpClient 调用 SSRS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20544796/