简体   繁体   English

来自 wcf 服务的 Httpwebrequest

[英]Httpwebrequest from wcf service

I am new to WCF, I have create one service for httpwebrequest to SSRS report and render the report in PDF or EXCEL format and save it to specific location on drive.我是 WCF 的新手,我为 httpwebrequest 到 SSRS 报告创建了一项服务,并以 PDF 或 EXCEL 格式呈现报告并将其保存到驱动器上的特定位置。

I am calling this service from web application on button click event.我在按钮单击事件中从 Web 应用程序调用此服务。 But it is giving an error on GetResponse()但是它在 GetResponse() 上给出了错误

The remote server returned an error: (403) Forbidden

Also, I have create same code in console application, It works perfect.另外,我在控制台应用程序中创建了相同的代码,它运行完美。

below is my code下面是我的代码

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)
            {
                //
            }
        }
    }

Below is fiddler details以下是小提琴手的详细信息

WCF hosted using IIS having error使用 IIS 托管的 WCF 有错误

Request Header请求头

GET /ReportServer2008?/ssrswcf/ssrswcftest&rs:Command=Render&rs:Format=PDF&sid=5 HTTP/1.1
Authorization: Negotiate some_long_string
Host: xyz

Response Header响应头

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 hosted using console application working perfect使用控制台应用程序托管的 WCF 完美运行

Request Header请求头

GET /ReportServer2008?/ssrswcf/ssrswcftest&rs:Command=Render&rs:Format=PDF&sid=5 HTTP/1.1
Authorization: Negotiate some_long_string
Host: xyz

Response Header响应头

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

Authorization: Negotiate indicates that authentication is in use. Authorization: Negotiate表示正在使用身份验证。 Probably, your WCF service does not have the required credentials.可能您的 WCF 服务没有所需的凭据。 Ask the service owner what authentication is required and configure it.询问服务所有者需要什么身份验证并进行配置。

It just need credential info, when it hosted from IIS.当它从 IIS 托管时,它只需要凭据信息。 it worked from console because that console application is executing as administrator.它从控制台工作,因为该控制台应用程序以管理员身份执行。

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)
            {
                //
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM