[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

ServerXMLHTTP problem

Using: ASP.NET, c#, msxml4 sp2

I'm trying to send an XML document to an apsx page using the following code:

ofRequest.open("POST", "http://www.sitename.com:82/ProcessOrder.aspx", true, null, null);
opDocOrder.setProperty("ServerHTTPRequest", true);
ofRequest.send(opDocOrder); // opDocOrder is an MSXML2.DOMDocument40 object

ofRequest is a ServerXMLHTTP40Class object

But am getting the following error message when ofRequest.send is invoked:

------------------------------

Access is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

----------------

It looks like a permissions problem, but I have no idea what I need to set permissions on, so any help would be greatly appreciated!

Simon
0
dhtml
Asked:
dhtml
1 Solution
 
rdcproCommented:
Why are you using MSXML 4 in ASP.NET?  This is pretty easy to do strictly in .NET.  I have a sample app that I use that demonstrates this.   A browser posts a form to the ASP.NET application, which constructs another form body (obviously it could be an XML document as well), and posts that to an ASP server that responds with the values that were posted.  The ASP.NET server then does a transfer and displays the values.  The actual point of the demo was how to progress from one ASP.NET page to another, but be able to read state from the previous page. However, it could all be done from the same page, and it would be simpler.  Here's the code:

WebForm1.aspx.cs  The ASPX page contains two text boxes and a submit button
==========================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace SandboxWebApp
{
      /// <summary>
      /// WebForm1 contains two text boxes.  User fills out each
      /// box, and then clicks Button1.  The Click event is processed,
      /// which constructs a POST operaton and makes a request of a
      /// separate server.  The response from the remote server is
      /// stored in a public property called RemoteResponse.  
      ///
      /// Then the page is transfered to WebForm2
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Label Label2;
            protected System.Web.UI.WebControls.TextBox skill;
            protected System.Web.UI.WebControls.TextBox level;
            protected System.Web.UI.WebControls.Button Button1;
            protected System.Web.UI.WebControls.Label target;
            protected System.Web.UI.WebControls.Label Label1;

            protected string remoteResponse;

            public string RemoteResponse
            {
                  get
                  {
                        return remoteResponse;
                  }
            }
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void Button1_Click(object sender, System.EventArgs e)
            {
                  // The client submit button POSTs the values to our server,
                  // which then needs to send those values to the remote
                  // server, get the results back, and then return a response
                  // to the client.

                  StringBuilder sb = new StringBuilder();
                  sb.Append("firstField=" + skill.Text);
                  sb.Append("&");
                  sb.Append("secondField=" + level.Text);




                  HttpWebRequest request =
                        (HttpWebRequest) WebRequest.Create("http://localhost:8181/COI/testResponse.asp");
                  request.Method = "POST";
                  request.ContentType = "application/x-www-form-urlencoded";

                  request.ContentLength = sb.Length;
                  try
                  {
                        StreamWriter sendStream = new StreamWriter(request.GetRequestStream());
                        sendStream.Write(sb.ToString());
                        sendStream.Close();
                  }
                  catch (Exception ex)
                  {
                        // Handle errors . . .
                        target.Text = "Error: " + ex.Message;
                  }
                  try
                  {
                        WebResponse response = request.GetResponse();
                   
                        StreamReader sr = new StreamReader(response.GetResponseStream());
                        remoteResponse = sr.ReadToEnd();
                        sr.Close();
                        Server.Transfer("WebForm2.aspx");
                  }
                  catch (Exception ex)
                  {
                        // Handle errors . . .
                        target.Text = "Error: " + ex.Message;
                  }

            }
      }
}


WebForm2.aspx.cs  ASPX page just contains a label for the response.
=====================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SandboxWebApp
{
      /// <summary>
      /// WebForm2 receives the transfer from WebForm1 and checks
      /// if we arrived here from WebForm1.  If so, the
      /// public property sourcePage.RemoteResponse is loaded into
      /// Label2.  If not, then somehow the browser got here by
      /// a route we do not support, so redirect back to the start
      /// page.
      /// </summary>
      public class WebForm2 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.Label Label2;
            protected WebForm1 sourcePage;


            private void Page_Load(object sender, System.EventArgs e)
            {
                  if (!Page.IsPostBack)
                  {
                        HttpContext myContext = HttpContext.Current;
                        // Did we get here from WebForm1?
                        if (myContext.Handler.GetType().BaseType == typeof(WebForm1))
                        {
                              sourcePage = (WebForm1)myContext.Handler;
                              Label2.Text = sourcePage.RemoteResponse;
                        }
                        else
                        {
                              myContext.Response.Redirect("WebForm1.aspx");
                        }
                  }
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}


Regards,
Mike Sharp
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now