2015-06-22 86 views
3

我是WCF的新手,我爲httpwebrequest創建了一個SSRS報告服務,並以PDF或EXCEL格式呈現報告並將其保存到驅動器上的特定位置。來自wcf服務的Httpwebrequest

我正在從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) 
      { 
       // 
      } 
     } 
    } 

下面是小提琴手細節

使用具有錯誤

請求頭

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

WCF託管

響應頭

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 
+0

我懷疑這是同樣的請求,出去...使用小提琴來攔截btoh請求並比較它們。也許一個Windows Auth的事情。 – usr

+0

感謝您的回覆,我會盡快回復您。 – Maddy

+0

我不確定,但小提琴手沒有顯示任何請求正在經歷。僅供參考,我曾嘗試從控制檯應用程序託管相同的wcf服務,然後它工作正常,但來自IIS的主機wcf存在問題。 – Maddy

回答

1

它只需要憑據信息,當它從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) 
      { 
       // 
      } 
     } 
    } 
1

Authorization: Negotiate indicates that authentication is in use.可能是,您的WCF服務沒有所需的憑據。詢問服務所有者需要什麼認證並對其進行配置。

+0

我託管該服務使用IIS和控制檯應用程序。爲Web應用程序調用該服務沒有問題。用於wcf服務的Httprequest存在問題。另外,我使用IIS和控制檯應用程序託管該服務。但只有IIS調用有錯誤。 – Maddy

+0

從控制檯應用程序調用成功並且從IIS託管的任何東西失敗時是不是正確? – boot4life

+0

謝謝你的工作,我明白了你的觀點。 – Maddy