We help IT Professionals succeed at work.

Using .Net to post xml to an ASP page, get a respone back in xml

Last Modified: 2013-11-19
This is continued from another question:


I'm trying to post xml to an asp page, then a response to the .net page.  
My .NET page:

<%@ Page Language="VB" Debug = true%>
<%@ Import namespace="System.Xml" %>
<%@ Import namespace="System.Web" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Collections" %>
 <%@ Import namespace="System.Diagnostics" %>
<%@ Import namespace="System.Net" %>
<script runat="server">

    ' Insert page code here

    Sub Button1_Click(sender As Object, e As EventArgs)
        ' Put user code to initialize the page here
                Dim xmlDoc As XmlDocument
                xmlDoc = New XmlDocument

                Dim result As String = ""
                Dim myWriter As StreamWriter
                Dim sURL = "https://npsonline.pti-nps.com/activate/activateservice1.asp"
                Dim sXML = "<?xml version='1.0' encoding='utf-8' ?><Root><groupnum>test</groupnum></Root>"
                Dim objRequest As HttpWebRequest = WebRequest.Create(sURL)
                objRequest.Method = "POST"
                objRequest.ContentLength = sXML.Length
                objRequest.ContentType = "application/x-www-form-urlencoded"

                    myWriter = New StreamWriter(objRequest.GetRequestStream())

                Catch e1 As Exception

                End Try

                Dim objResponse As HttpWebResponse = objRequest.GetResponse()
                Dim sr As StreamReader
                sr = New StreamReader(objResponse.GetResponseStream())
                result = sr.ReadToEnd()
                'Xml1.Document = xmlDoc

    End Sub

    <form runat="server">
        <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        <!-- Insert content here -->

The ASP page receiving it:

<% @Language="vbscript" %>
     Response.ContentType = "Text/XML"
     Set req = server.CreateObject("MSXML.DOMDocument")
      i = req.load(Request)
      if i then
          set mynode=req.SelectSingleNode("Root/groupnum")
          groupnum = myNode.Text
          set mynode=req.SelectSingleNode("Root/controlnum")
          controlnum = myNode.Text
          Response.Write("<?xml version='1.0' encoding='utf-8' ?> ")
          Response.Write("<Root>" & groupnum  &  " </Root>")
                Response.Write("<?xml version='1.0' encoding='utf-8' ?> ")
         Response.Write("<Root>" & req.ParseError.Reason &  " </Root>")
     End if


My error:  

Line 37:                 End Try
Line 38:
Line 39:                 Dim objResponse As HttpWebResponse = objRequest.GetResponse()
Line 40:                 Dim sr As StreamReader
Line 41:                 sr = New StreamReader(objResponse.GetResponseStream())

Source File: C:\Inetpub\wwwroot\websvc\test.aspx    Line: 39

Stack Trace:

[WebException: The remote server returned an error: (500) Internal Server Error.]
   System.Net.HttpWebRequest.CheckFinalStatus() +674
   System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +139
   System.Net.HttpWebRequest.GetResponse() +149
   ASP.test_aspx.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\websvc\test.aspx:39
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +83
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277
Watch Question

You are not supplying a controlnum element with XML you are sending, hence the ASP page is blowing up


That appears to have done it.  When I run the .NET page on my local machine and post to the asp page on a different web server, it returns what I'm looking for.   The other company posting to it though took my example .NET code and modified it to use C#, and got the following error:

System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Only thing I can think of w/out seeing his code is that he erred on the implementation of it somehow--since mine works fine.   Of course, both of my machines are on the same network...

Anyway, can you tell me is there a log somewhere to see what he posted, and what the error was that he got in return in more detail?
You have no "Root/controlnum" in request xml. maybe this is a problem


Actually, I had changed it per Dfiala's suggestion right before my last post:

      if i then
          set mynode=req.SelectSingleNode("Root/groupnum")
          groupnum = myNode.Text
          set mynode=req.SelectSingleNode("Root/controlnum")
          controlnum = myNode.Text

          Response.Write("<?xml version='1.0' encoding='utf-8' ?> ")
          Response.Write("<Root>" & groupnum  & controlnum & " </Root>")
          Response.Write("<?xml version='1.0' encoding='utf-8' ?> ")
          Response.Write("<Root>" & req.ParseError.Reason &  " </Root>")
     End if

That was causing an error, but now I'm pulling controlnum from the xml.   I am then just sending a test response back--after it's verified that it works, we'll change it to do the other processing and sending of a return message that means more.  

You have to create your own logging in ASP to get what values were sent in.

Just write out the Request to a file before you do anything else.


Lol....that's what I'm doing now as we speak.   THanks for the suggestions, though.    You've been a huge help.  
Unlock this solution and get a sample of our free trial.
(No credit card required)


dude, I can't thank you enough for helping me figure this out.    Many, many, many, many thanks.    My user figured it out.  His c# code that was calling it was using an lowercase R instead of uppercase.   The xml message appeared incorrect--because of the case sensitivity thing.  

It's all good.   But I'm still going to tell my boss we need to get out of the dark ages of classic ASP and move to .NET.  

Glad you got it working, glad I coud help.  Yes, ASP.NET makes life a lot easier.

Have fun.


 I have the same problem, But I am working on windows service...
 I  have following code which works very fine in case of windows application, the moment i put the code in windows service, windows service give me an error.  I am traping error in txt file it says.

 The underlying connection was closed: The remote name could not be resolved.===   at    
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
   at System.Net.HttpWebRequest.GetRequestStream()
   at System.Net.WebClient.UploadData(String address, String method, Byte[] data)
   at WindowsService1.Service1.OnStart(String[] args)

 My .Net \Code is as follows :
 what is wrong with Windows Service
string SMSUrl = @"http://www.zipsms.com/sski/XmlPost.asp?uid=sski&upwd=sski$$";
      string LOGIN_URL  = @"http://url/XmlPost.asp?uid=uid&upwd=pwd";
      string MessageStart = "<?xml version=\"1.0\"?><numbers><number>1234</number><message>my tesx</message></numbers>";
      string postData = MessageStart;
      StreamWriter wr = new StreamWriter(@"C:\test.txt",true);
int i=0;
      for (i=0;i<=5;i++)
      WebClient cl = new WebClient();
      byte[] response;
      cl.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
      response = cl.UploadData(LOGIN_URL, "POST", Encoding.ASCII.GetBytes(postData));            cl.Dispose();                              
      wr.Write("The value of i is " + i.ToString());
catch(Exception ex)
      wr.Write("Error has ocurred for follwoing No : " +i.ToString() );
      wr.Write("Actaul Error Is : " +ex.Message + "===" + ex.StackTrace  );

 Thanks and Regards,
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.