来自wcf服务的Httpwebrequest

本文关键字:Httpwebrequest 服务 wcf 来自 | 更新日期: 2023-09-27 17:57:27

我是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

来自wcf服务的Httpwebrequest

Authorization: Negotiate表示正在使用身份验证。您的WCF服务可能没有所需的凭据。询问服务所有者需要什么身份验证并进行配置。

当它从IIS托管时,只需要凭据信息。它在控制台上工作,因为控制台应用程序是以管理员身份执行的。

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.Credentials = new NetworkCredential(@"username", "password"); 
                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)
            {
                //
            }
        }
    }