【发布时间】:2015-09-07 23:06:11
【问题描述】:
我是 WCF 的新手,我为 httpwebrequest 创建了一项服务到 SSRS 报告并以 PDF 或 EXCEL 格式呈现报告并将其保存到驱动器上的特定位置。
我在按钮单击事件中从 Web 应用程序调用此服务。但它在 GetResponse() 上给出错误
The remote server returned an error: (403) Forbidden
另外,我在控制台应用程序中创建了相同的代码,效果很好。
下面是我的代码
public class ReportGenerator : IReportGenerator
{
public void ReportRequest()
{
try
{
string URL = "http://localhost/ReportServer2008?/ssrswcf/ssrswcftest";
string Command = "Render";
string Format = "PDF";//"EXCEL"
URL = URL + "&rs:Command=" + Command + "&rs:Format=" + Format + "&sid=5";
System.Net.HttpWebRequest Req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);
Req.UseDefaultCredentials = true;
Req.Method = "GET";
string path = @"C:\ssrswcftest\" + Convert.ToString(Guid.NewGuid()) + @".pdf";
System.Net.WebResponse objResponse = Req.GetResponse();
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);
System.IO.Stream stream = objResponse.GetResponseStream();
byte[] buf = new byte[1024];
int len = stream.Read(buf, 0, 1024);
while (len > 0)
{
fs.Write(buf, 0, len);
len = stream.Read(buf, 0, 1024);
}
stream.Close();
fs.Close();
}
catch (WebException ex)
{
//
}
catch (Exception ex)
{
//
}
}
}
以下是提琴手详情
使用 IIS 托管的 WCF 出现错误
请求标头
GET /ReportServer2008?/ssrswcf/ssrswcftest&rs:Command=Render&rs:Format=PDF&sid=5 HTTP/1.1
Authorization: Negotiate some_long_string
Host: xyz
响应标头
HTTP/1.1 403 Forbidden
Cache-Control: private
Content-Length: 2925
Content-Type: text/html; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
X-AspNet-Version: 2.0.50727
Date: Mon, 22 Jun 2015 15:39:29 GMT
使用控制台应用程序托管的 WCF 运行良好
请求标头
GET /ReportServer2008?/ssrswcf/ssrswcftest&rs:Command=Render&rs:Format=PDF&sid=5 HTTP/1.1
Authorization: Negotiate some_long_string
Host: xyz
响应标头
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 25653
Content-Type: application/pdf
Expires: Mon, 22 Jun 2015 16:16:42 GMT
Last-Modified: Mon, 22 Jun 2015 16:17:43 GMT
Set-Cookie: RSExecutionSession%3a%2fssrswcf%2fssrswcftest=aywu4s45sefnmw45z50bn2vh; path=/
Server: Microsoft-HTTPAPI/2.0
X-AspNet-Version: 2.0.50727
FileExtension: pdf
Content-Disposition: attachment; filename="ssrswcftest.pdf"
Date: Mon, 22 Jun 2015 16:17:42 GMT
【问题讨论】:
-
我怀疑发出的请求是同一个请求...使用 Fiddler 拦截 btoh 请求并进行比较。也许是 Windows Auth 的事情。
-
感谢您的回复,我会马上回复您。
-
我不确定,但提琴手没有显示任何请求正在通过。仅供参考,我尝试从控制台应用程序托管相同的 wcf 服务,然后它工作正常,但从 IIS 托管 wcf 有问题。
-
Fiddler 只为当前用户捕获。您需要在 web.config 中将其设置为代理(或等效项)。
-
我已经更新了提琴手详细信息的问题,如果您有任何想法,请告诉我,谢谢
标签: c# asp.net wcf reporting-services httpwebrequest