title: How to resolve "The remote server returned an error: (401) Unauthorized" when running a SQL Server Reporting Service?
body: <p>I'm using this <a href="http://www.codeproject.com/Articles/25994/Creating-a-Proxy-to-Download-PDF-Reports-from-SRS" rel="nofollow">code</a> to create and download PDF reports from SSRS, but I receive the following error:</p>
  
  <blockquote>
    <p>The remote server returned an error: (401) Unauthorized. </p>
  </blockquote>
  
  <p>My code is:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      MemoryStream ms;
      // this name is what the user will see when they are prompted for download.
      string customFileName = "NewFileName.pdf";
  
      if (!this.IsPostBack)
      {
          try
          {
              string strRequest = "http://myServer.com/ReportServer?%2fmetadata_report%2fMetadataReport1&amp;rs%3aCommand=Render&amp;rs%3AFormat=PDF&amp;grpId=3";
              WebRequest request = WebRequest.Create(strRequest);
              request.ContentType = "application/pdf";
  
              string userName = "username";
              string password = "password";
              string domain = "myServer.com";
  
              // Create and then pass in network credentials to acecss the reporting server
              System.Net.NetworkCredential credentials = new NetworkCredential(userName, password, domain);
              request.Credentials = credentials;
  
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  
              using (response)
              {
                  // Get the stream from the server
                  using (Stream stream = response.GetResponseStream())
                  {
                      // Use the ReadFully method from the link above:
                      byte[] data = ReadFully(stream, response.ContentLength);
  
                      // Return the memory stream.
                      ms = new MemoryStream(data);
                  }
              }
  
              // Clear out the headers that may already exist, then set content type and add a header to name the file
              Response.Clear();
              Response.ContentType = "application/pdf";
              Response.AddHeader("content-disposition", "inline; ");
  
              //// Write the memory stream containing the pdf file directly to the Response object that gets sent to the client
              ms.WriteTo(Response.OutputStream);
          }
      }
  }
  </code></pre>
  
  <p>Any help would be much appreciated.</p>