C# -HttpWebRequest returns 400 以编程方式连接到报表服务器时请求错误

C# -HttpWebRequest returns 400 Bad request when connecting to Report Server programmatically

我正在以编程方式从 asp 网页向报表服务器发出 Http Web 请求,该网页存在于另一台远程机器上以下载报表(URL 访问 - SSRS)

当我直接输入 URL 时,浏览器询问我凭据,当我输入凭据时,报告会自动下载到浏览器中, 但是当我以编程方式发出请求时,出现了下面提到的错误。

C# 代码:

string username="abc", password="123";
string url = @"http://ServerName/ReportingPath&Parameter1=123&rs:Format=PDF";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));                
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);                
request.Headers.Add("Authorization", "Basic " + encoded);
request.PreAuthenticate = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 11.0; Windows NT 6.0; WOW64; " +
                "Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; " +  ".NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; " +
                "InfoPath.2)";                
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

当我提供 "domain/abc" 或 "abc@domain" 用户名时,我收到 HTTP 400 错误请求, 当我放置此语句时它也会出现

request.Credentials = new NetworkCredential(username,password,domain);

而不是行->

request.Headers.Add("Authorization", "Basic " + encoded);

如果我简单地提供用户名,剥离域,比如用户名="abc",我会收到 401-Unauthorised 错误。

当我在互联网浏览器中输入 URL 时,我没有收到错误 400 或 401。当它要求凭据时,我将用户名指定为 "domain/abc",将密码指定为“123”

我的报表服务器的webConfig文件是这样的..

<Authentication>
<AuthenticationTypes>
        <RSWindowsBasic/>
</AuthenticationTypes>
<RSWindowsExtendedProtectionLevel>Off</RSWindowsExtendedProtectionLevel>
<RSWindowsExtendedProtectionScenario>Proxy</RSWindowsExtendedProtectionScenario>
<EnableAuthPersistence>true</EnableAuthPersistence>
</Authentication>

如有任何帮助,我们将不胜感激。

你可以尝试将 url 设置为 http://abc:123@ServerName/ReportingPath&Parameter1=123&rs:Format=PDF 并且没有验证。

它是 HTTP basic authentication 的一部分。