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.Collecti ons" %>
<%@ Import namespace="System.Diagnost ics" %>
<%@ 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</gr oupnum></R oot>"
Dim objRequest As HttpWebRequest = WebRequest.Create(sURL)
objRequest.Method = "POST"
objRequest.ContentLength = sXML.Length
objRequest.ContentType = "application/x-www-form-ur lencoded"
Try
myWriter = New StreamWriter(objRequest.Ge tRequestSt ream())
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.G etResponse Stream())
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 .DOMDocume nt")
req.async=false
i = req.load(Request)
if i then
set mynode=req.SelectSingleNod e("Root/gr oupnum")
groupnum = myNode.Text
set mynode=req.SelectSingleNod e("Root/co ntrolnum")
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.G etResponse Stream())
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. CheckFinal Status() +674
System.Net.HttpWebRequest. EndGetResp onse(IAsyn cResult asyncResult) +139
System.Net.HttpWebRequest. GetRespons e() +149
ASP.test_aspx.Button1_Clic k(Object sender, EventArgs e) in C:\Inetpub\wwwroot\websvc\ test.aspx: 39
System.Web.UI.WebControls. Button.OnC lick(Event Args e) +83
System.Web.UI.WebControls. Button.Sys tem.Web.UI .IPostBack EventHandl er.RaisePo stBackEven t(String eventArgument) +57
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData) +33
System.Web.UI.Page.Process RequestMai n() +1277
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.Collecti
<%@ Import namespace="System.Diagnost
<%@ 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</gr
Dim objRequest As HttpWebRequest = WebRequest.Create(sURL)
objRequest.Method = "POST"
objRequest.ContentLength = sXML.Length
objRequest.ContentType = "application/x-www-form-ur
Try
myWriter = New StreamWriter(objRequest.Ge
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.G
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
req.async=false
i = req.load(Request)
if i then
set mynode=req.SelectSingleNod
groupnum = myNode.Text
set mynode=req.SelectSingleNod
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.G
Source File: C:\Inetpub\wwwroot\websvc\
Stack Trace:
[WebException: The remote server returned an error: (500) Internal Server Error.]
System.Net.HttpWebRequest.
System.Net.HttpWebRequest.
System.Net.HttpWebRequest.
ASP.test_aspx.Button1_Clic
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
You are not supplying a controlnum element with XML you are sending, hence the ASP page is blowing up
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?
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
ASKER
Actually, I had changed it per Dfiala's suggestion right before my last post:
if i then
set mynode=req.SelectSingleNod e("Root/gr oupnum")
groupnum = myNode.Text
set mynode=req.SelectSingleNod e("Root/co ntrolnum")
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.
if i then
set mynode=req.SelectSingleNod
groupnum = myNode.Text
set mynode=req.SelectSingleNod
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.
Just write out the Request to a file before you do anything else.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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.
at System.Net.HttpWebRequest.
at System.Net.HttpWebRequest.
at System.Net.WebClient.Uploa
at WindowsService1.Service1.O
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>
string postData = MessageStart;
StreamWriter wr = new StreamWriter(@"C:\test.txt
int i=0;
try
{
for (i=0;i<=5;i++)
{
WebClient cl = new WebClient();
byte[] response;
cl.Headers.Add("Content-Ty
response = cl.UploadData(LOGIN_URL, "POST", Encoding.ASCII.GetBytes(po
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