簡體   English   中英

來自 wcf 服務的 Httpwebrequest

[英]Httpwebrequest from wcf service

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM