Httpwebrequest from wcf service

1.1k Views Asked by At

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.

I am calling this service from web application on button click event. But it is giving an error on 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

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

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
2

There are 2 best solutions below

0
On BEST ANSWER

It just need credential info, when it hosted from 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)
            {
                //
            }
        }
    }
2
On

Authorization: Negotiate indicates that authentication is in use. Probably, your WCF service does not have the required credentials. Ask the service owner what authentication is required and configure it.