HTTP Response
-
I'm relatively new to dealing with internet based ASP.net, (I've been used to internal only Intranet development until recently) and am looking into building an Application that posts XML to an external site, which will parse and validate the data and return the HTTP Response Code. This side seems OK. What will later happen is that the XML will be further amended etc. then posted to a URL within our site. But I'm having problems reading the posted XML (HTTPResponse ?) and outputting it to screen or file. I'm really looking for opinions and help with the most practical method of reading the posted XML, saving it to a file and displaying the content on the page. Rhys "When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas "As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates "Doh!" -- Homer Simpson
-
I'm relatively new to dealing with internet based ASP.net, (I've been used to internal only Intranet development until recently) and am looking into building an Application that posts XML to an external site, which will parse and validate the data and return the HTTP Response Code. This side seems OK. What will later happen is that the XML will be further amended etc. then posted to a URL within our site. But I'm having problems reading the posted XML (HTTPResponse ?) and outputting it to screen or file. I'm really looking for opinions and help with the most practical method of reading the posted XML, saving it to a file and displaying the content on the page. Rhys "When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas "As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates "Doh!" -- Homer Simpson
Hi Rhys. When you say the xml is posted to the server, do you literally mean through a form POST method? or is it being submitted to the server through some other method (e.g. as a SOAP call to a web service?)
-
Hi Rhys. When you say the xml is posted to the server, do you literally mean through a form POST method? or is it being submitted to the server through some other method (e.g. as a SOAP call to a web service?)
Through the Post method. I've knocked up a traditional ASP workaround by reading the Binary of the Request object and using a Binary to String conversion function to output the XML using standard File IO scripting objects, but would be happier being able to do this in .Net really just for efficiency sake if nothing else. Rhys "When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas "As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates "Doh!" -- Homer Simpson
-
Through the Post method. I've knocked up a traditional ASP workaround by reading the Binary of the Request object and using a Binary to String conversion function to output the XML using standard File IO scripting objects, but would be happier being able to do this in .Net really just for efficiency sake if nothing else. Rhys "When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas "As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates "Doh!" -- Homer Simpson
Hi Rhys. Well, you should still be able to get the posted text through the
Request
property of thePage
. Then with that string, you could instantiate anXmlDocument
object, or write the text as is to a file, even supply the text as source for anXML
control on the web page for XSL transformation to HTML. Here's a quick'n'dirty example of taking a POSTed string value and saving it to a text file in .NET:using System.IO;
void CreateMyFile(sFileName as string)
{
// assuming the POST variable is 'myTextBox'...
string sText = Request["myTextBox"];using (StreamWriter sw = File.CreateText(sFileName)) { sw.Write(sText); }
}
If you wanted to manipulate the POSTed string more like an
XmlDocument
, you could do something like this:using System.Xml;
void DoSomethingWithXmlDocument()
{
// assuming the POST variable is 'myTextBox'...
string sText = Request["myTextBox"];XmlDocument doc = new XmlDocument(); doc.LoadXml(sText); // do something with the XmlDocument... // ...
}
And here is a quick example for using the POSTed value with an Xml control:
<script runat="server">
void Page_Load(object o, EventArgs e)
{
string sText = Request["myTextBox"];
XmlDocument doc = new XmlDocument();
doc.LoadXml(sText);
myXml.Document = doc;
}
</script><html>
<head></head>
<body>
<asp:Xml id="myXml" runat="server"
TransformSource="myStylesheet.xsl" />
</body>
</html>I hope this helps.
-
Hi Rhys. Well, you should still be able to get the posted text through the
Request
property of thePage
. Then with that string, you could instantiate anXmlDocument
object, or write the text as is to a file, even supply the text as source for anXML
control on the web page for XSL transformation to HTML. Here's a quick'n'dirty example of taking a POSTed string value and saving it to a text file in .NET:using System.IO;
void CreateMyFile(sFileName as string)
{
// assuming the POST variable is 'myTextBox'...
string sText = Request["myTextBox"];using (StreamWriter sw = File.CreateText(sFileName)) { sw.Write(sText); }
}
If you wanted to manipulate the POSTed string more like an
XmlDocument
, you could do something like this:using System.Xml;
void DoSomethingWithXmlDocument()
{
// assuming the POST variable is 'myTextBox'...
string sText = Request["myTextBox"];XmlDocument doc = new XmlDocument(); doc.LoadXml(sText); // do something with the XmlDocument... // ...
}
And here is a quick example for using the POSTed value with an Xml control:
<script runat="server">
void Page_Load(object o, EventArgs e)
{
string sText = Request["myTextBox"];
XmlDocument doc = new XmlDocument();
doc.LoadXml(sText);
myXml.Document = doc;
}
</script><html>
<head></head>
<body>
<asp:Xml id="myXml" runat="server"
TransformSource="myStylesheet.xsl" />
</body>
</html>I hope this helps.
Our problem is that the Xml strng posted to the page will be from another site and we can't control how, so we really need to grad the page content. At the moment I've knocked up a classic asp workaround by getting the binary of the Request object, then using a binary to string function i turn that back into a string and save it file IO, (unfortunately due to a bug in the Ms Xml (v3 and v4) libraries using an XmlDocument object generates an erroneous error as we have a element). Basically this is what I've got going on...
Option Explicit Dim strABBXMLFile strABBXMLFile = Server.MapPath("/" & Day(Now) & "-" & Month(Now) & "-" & Year(Now) & "_" & Hour(Now) & Minute(Now) & ".xml") 'FileIO START Dim fso, f, strRequest dim a,b a = Request.TotalBytes strRequest = BinaryToString(Request.BinaryRead(a)) Set fso = Server.CreateObject("Scripting.FileSystemObject") Set f = fso.CreateTextFile(strABBXMLFile) f.WriteLine(strRequest) f.close Set f = Nothing Set fso = Nothing 'FileIO END
Which does the trick but it does seem like a bit of a hacky way of doing it. Can I grab the entire page in Asp.net (c# or vb)? Rhys "When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas "As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates "Doh!" -- Homer Simpson -
Our problem is that the Xml strng posted to the page will be from another site and we can't control how, so we really need to grad the page content. At the moment I've knocked up a classic asp workaround by getting the binary of the Request object, then using a binary to string function i turn that back into a string and save it file IO, (unfortunately due to a bug in the Ms Xml (v3 and v4) libraries using an XmlDocument object generates an erroneous error as we have a element). Basically this is what I've got going on...
Option Explicit Dim strABBXMLFile strABBXMLFile = Server.MapPath("/" & Day(Now) & "-" & Month(Now) & "-" & Year(Now) & "_" & Hour(Now) & Minute(Now) & ".xml") 'FileIO START Dim fso, f, strRequest dim a,b a = Request.TotalBytes strRequest = BinaryToString(Request.BinaryRead(a)) Set fso = Server.CreateObject("Scripting.FileSystemObject") Set f = fso.CreateTextFile(strABBXMLFile) f.WriteLine(strRequest) f.close Set f = Nothing Set fso = Nothing 'FileIO END
Which does the trick but it does seem like a bit of a hacky way of doing it. Can I grab the entire page in Asp.net (c# or vb)? Rhys "When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas "As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates "Doh!" -- Homer SimpsonHi Rhys. Okay - take a look at the HttpRequest.InputStream[^] property. I think this may be something you can use. The example on MSDN shows how to take the full
Request
stream and convert it to a string.