Link to home
Start Free TrialLog in
Avatar of keith_dude
keith_dude

asked on

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

This is continued from another question:

https://www.experts-exchange.com/questions/20931704/Post-to-asp-xmlhttp-from-Aspx-and-consume-xml-response.html#11007196

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"

                Try
                    myWriter = New StreamWriter(objRequest.GetRequestStream())
                    myWriter.Write(sXML)
                    myWriter.Flush()
                    myWriter.Close()

                Catch e1 As Exception
                    Debug.Write(e1.Message)
                Finally

                End Try

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


    End Sub
</script>

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


The ASP page receiving it:

<% @Language="vbscript" %>
<%
     Response.ContentType = "Text/XML"
     Set req = server.CreateObject("MSXML.DOMDocument")
     req.async=false
      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>")
      else
     
                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
Avatar of dfiala13
dfiala13

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

ASKER

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>")
      else
     
          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.  
ASKER CERTIFIED SOLUTION
Avatar of dfiala13
dfiala13

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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    
System.Net.HttpWebRequest.CheckFinalStatus()
   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;
try
{            
      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  );
}
finally
{
                wr.Close();
}

 Thanks and Regards,
 Shailesh