Retrieving values from HTTP POST

G

Guest

I am wanting to capture the XML posted by an InfoPath form with .NET, but I
cannot figure out how to capture the XML stream sent back by the InfoPath
form. With Classic ASP, I could just create an MSXML object then load the XML
with a simple objXML.Load(Request). This would give me the XML stream, which
I could then manipulate.

However, using System.Xml, this is not possible - not that I have found,
anyway. The InfoPath documentation simply states that "InfoPath sends the
form as XML data in the body of an HTTP POST request to the specified URL".
But how do I access that data? Any help would be greatly appreciated.
 
L

Lucas Tam

However, using System.Xml, this is not possible - not that I have
found, anyway. The InfoPath documentation simply states that "InfoPath
sends the form as XML data in the body of an HTTP POST request to the
specified URL". But how do I access that data? Any help would be
greatly appreciated.

Check the body... is it a hidden form field?

If it is, you can use Request.Form("FormName") to retrieve the value of the
XML.
 
G

Guest

Hi,

In ASP.Net, you can use the System.XML.XmlDocument object for working on the
xml instead of the MSXML.

code snippet for loading the xml from the request:

System.XML.XmlDocument xDoc = new System.XML.XmlDocument();
xDoc.Load(Request.InputStream);
//Now, manupulate the xDoc
 
J

John Smith

Running the code objXMLDoc.Load(Request.InputStream) returns the
error: convert from 'System.IO.Stream' to 'string'. I've added
'.ToString()', but that results in just the text string
'System.Web.HttpInputStream', and not the actual XML.



On Sun, 14 Aug 2005 07:15:03 -0700, "Saravanan K V"

:Hi,
:
:In ASP.Net, you can use the System.XML.XmlDocument object for working on the
:xml instead of the MSXML.
:
:code snippet for loading the xml from the request:
:
:System.XML.XmlDocument xDoc = new System.XML.XmlDocument();
:xDoc.Load(Request.InputStream);
://Now, manupulate the xDoc
 
S

Steven Cheng[MSFT]

Hi John,

What's the "objXMLDoc" in your code? Is it the MSXML's DOMDocument or the
..NET's
System.Xml.XmlDocument? Since we're not supported to use the COM msxml
component in .NET application, we should use the System.Xml.XmlDocument to
capture xml data. Here is the test code snippet I use in my ASP.NET web
page:

private void Page_Load(object sender, System.EventArgs e)
{
if(Request.ContentType == "text/xml")
{
XmlDocument doc = new XmlDocument();
doc.Load(Request.InputStream);

Response.Write(HttpUtility.HtmlEncode(doc.OuterXml));

Response.End();
}
}

Hope helps. Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





--------------------
| NNTP-Posting-Date: Mon, 15 Aug 2005 11:31:02 -0500
| From: John Smith <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Re: Retrieving values from HTTP POST
| Date: Mon, 15 Aug 2005 11:31:02 -0500
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
| X-Newsreader: Forte Agent 2.0/32.652
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Lines: 21
| NNTP-Posting-Host: 69.137.71.225
| X-Trace:
sv3-adqGpeoGmhDDgmTv1+mHE3QzJjsbKHGA0YanckV+sUpOZ2sdLt8OmviM9pfiRjuOXSUYdtFK
tZlNwD9!NO+LHmQaeH5ZCx83oN2WbvMdjnUEI1wWcGsNnz8ZktAlQKGvWxeegGuRNXSHpDaClBSg
lM/BzNpi!LF507AX/gtlkW6p07w==
| X-Complaints-To: (e-mail address removed)
| X-DMCA-Complaints-To: (e-mail address removed)
| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
complaint properly
| X-Postfilter: 1.3.32
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fastwebnet.it!tiscali!ne
wsfeed1.ip.tiscali.net!news.maxwell.syr.edu!border1.nntp.dca.giganews.com!lo
cal01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for
-mail
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118025
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Running the code objXMLDoc.Load(Request.InputStream) returns the
| error: convert from 'System.IO.Stream' to 'string'. I've added
| '.ToString()', but that results in just the text string
| 'System.Web.HttpInputStream', and not the actual XML.
|
|
|
| On Sun, 14 Aug 2005 07:15:03 -0700, "Saravanan K V"
|
| :Hi,
| :
| :In ASP.Net, you can use the System.XML.XmlDocument object for working on
the
| :xml instead of the MSXML.
| :
| :code snippet for loading the xml from the request:
| :
| :System.XML.XmlDocument xDoc = new System.XML.XmlDocument();
| :xDoc.Load(Request.InputStream);
| ://Now, manupulate the xDoc
|
|
 
J

Joerg Jooss

Lucas said:
Check the body... is it a hidden form field?

If it is, you can use Request.Form("FormName") to retrieve the value
of the XML.

I guess it's a straight "as is" POST. In this case, you can read the
XML from Request.InputStream.

Cheers,
 
J

John Smith

I think I found my issue. The XML is coming from an InfoPath form, and
the stream has headers (e.g., <? xml version 1.0 > and some other
MS-related tags at the very top). I think this is messing up the Load
call.




:Hi John,
:
:What's the "objXMLDoc" in your code? Is it the MSXML's DOMDocument or the
:.NET's
:System.Xml.XmlDocument? Since we're not supported to use the COM msxml
:component in .NET application, we should use the System.Xml.XmlDocument to
:capture xml data. Here is the test code snippet I use in my ASP.NET web
:page:
:
:private void Page_Load(object sender, System.EventArgs e)
:{
: if(Request.ContentType == "text/xml")
: {
: XmlDocument doc = new XmlDocument();
: doc.Load(Request.InputStream);
:
: Response.Write(HttpUtility.HtmlEncode(doc.OuterXml));
:
: Response.End();
: }
:}
:
:Hope helps. Thanks,
:
:Regards,
:
:Steven Cheng
:Microsoft Online Support
:
:Get Secure! www.microsoft.com/security
:(This posting is provided "AS IS", with no warranties, and confers no
:rights.)
:
:
:
:
:
:--------------------
:| NNTP-Posting-Date: Mon, 15 Aug 2005 11:31:02 -0500
:| From: John Smith <[email protected]>
:| Newsgroups: microsoft.public.dotnet.framework.aspnet
:| Subject: Re: Retrieving values from HTTP POST
:| Date: Mon, 15 Aug 2005 11:31:02 -0500
:| Message-ID: <[email protected]>
:| References: <[email protected]>
:<[email protected]>
:| X-Newsreader: Forte Agent 2.0/32.652
:| MIME-Version: 1.0
:| Content-Type: text/plain; charset=us-ascii
:| Content-Transfer-Encoding: 7bit
:| Lines: 21
:| NNTP-Posting-Host: 69.137.71.225
:| X-Trace:
:sv3-adqGpeoGmhDDgmTv1+mHE3QzJjsbKHGA0YanckV+sUpOZ2sdLt8OmviM9pfiRjuOXSUYdtFK
:tZlNwD9!NO+LHmQaeH5ZCx83oN2WbvMdjnUEI1wWcGsNnz8ZktAlQKGvWxeegGuRNXSHpDaClBSg
:lM/BzNpi!LF507AX/gtlkW6p07w==
:| X-Complaints-To: (e-mail address removed)
:| X-DMCA-Complaints-To: (e-mail address removed)
:| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
:| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
:complaint properly
:| X-Postfilter: 1.3.32
:| Path:
:TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fastwebnet.it!tiscali!ne
:wsfeed1.ip.tiscali.net!news.maxwell.syr.edu!border1.nntp.dca.giganews.com!lo
:cal01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for
:-mail
:| Xref: TK2MSFTNGXA01.phx.gbl
:microsoft.public.dotnet.framework.aspnet:118025
:| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
:|
:| Running the code objXMLDoc.Load(Request.InputStream) returns the
:| error: convert from 'System.IO.Stream' to 'string'. I've added
:| '.ToString()', but that results in just the text string
:| 'System.Web.HttpInputStream', and not the actual XML.
:|
:|
:|
:| On Sun, 14 Aug 2005 07:15:03 -0700, "Saravanan K V"
:|
:| :Hi,
:| :
:| :In ASP.Net, you can use the System.XML.XmlDocument object for working on
:the
:| :xml instead of the MSXML.
:| :
:| :code snippet for loading the xml from the request:
:| :
:| :System.XML.XmlDocument xDoc = new System.XML.XmlDocument();
:| :xDoc.Load(Request.InputStream);
:| ://Now, manupulate the xDoc
:|
:|
 
R

RHPT

Here's something strange:

On default.aspx, I have a textbox that I fill with the following XML
string and then submit:

<myFields><txt_name>adsf</txt_name><gender_buttons>Male</gender_buttons><birthday>2005-08-17</birthday><spam_me>true</spam_me></myFields>

On my code behind page, I have the following code:

XmlDocument doc = new XmlDocument();
doc.LoadXml(Request.InputStream.ToString());
doc.Save("C:\\Inetpub\\wwwroot\\RX2\\test.xml");

When I debug into the code, Request.InputStream.ToString() returns
"System.Web.HttpInputStream".

I've debugged intot he code, and looked at the Request object. Nowhere
does it have the XML.
 
S

Steven Cheng[MSFT]

Hi RHPT,

You means the XML content in your request are coming from the clientside
input in a TextBox? If so, I don't think we can directly use the
Request.InputStream to load the Xml content since the whole http message's
contentType is event not pure XML. We can load XmlDocument directly from
the Request.InputStream only if the pure request message is "text/xml"
contentType. For example, we can send XML content to a remote page through
the following code:

==============
static void Post_XML()
{
string url = "http://remoteserver/app/remotepage.aspx";

HttpWebRequest webreq = WebRequest.Create(url) as HttpWebRequest;

webreq.Method = "POST";
webreq.ContentType = "text/xml";


XmlDocument doc = new XmlDocument();
doc.LoadXml(
@"<?xml version='1.0' ?>
<root>
<items>
<item id='1'>item1</item>
<item id='2'>item2</item>
<item id='3'>item3</item>
<item id='4'>item4</item>
</items>
</root>
"
);

StreamWriter sw = new StreamWriter(webreq.GetRequestStream(),
System.Text.Encoding.UTF8);
sw.Write(doc.OuterXml);
sw.Close();

HttpWebResponse webrep = webreq.GetResponse() as HttpWebResponse;
StreamReader sr = new
StreamReader(webrep.GetResponseStream(),System.Text.Encoding.UTF8);

Console.WriteLine(sr.ReadToEnd());
sr.Close();
webrep.Close();

}
===========================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| NNTP-Posting-Date: Thu, 18 Aug 2005 20:35:01 -0500
| From: RHPT <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Re: Retrieving values from HTTP POST
| Date: Thu, 18 Aug 2005 20:34:57 -0500
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| X-Newsreader: Forte Agent 2.0/32.652
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Lines: 21
| NNTP-Posting-Host: 69.137.71.225
| X-Trace:
sv3-tqkcQ2XW4EE9xw18jiJjXuANveHVgbfoc05N5egU8S4IaI6IrVyNbV/YEaLlsoyLoC+VJvUw
0DGWkFd!QIWeCmsrkJS0gql7mj+eeXKYNjlQyKqWTL1VrYRwVeYo45DLITxDL3JenJbf86cv9ddq
MSJcW+yb!NbDf2F9l68qFqzNhrw==
| X-Complaints-To: (e-mail address removed)
| X-DMCA-Complaints-To: (e-mail address removed)
| X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
complaint properly
| X-Postfilter: 1.3.32
| Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!border2.nntp.dca.giganews.com!nntp.giganews.com!border1.nntp.dca.gigan
ews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POST
ED!not-for-mail
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:119021
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Here's something strange:
|
| On default.aspx, I have a textbox that I fill with the following XML
| string and then submit:
|
|
<myFields><txt_name>adsf</txt_name><gender_buttons>Male</gender_buttons><bir
thday>2005-08-17</birthday><spam_me>true</spam_me></myFields>
|
| On my code behind page, I have the following code:
|
| XmlDocument doc = new XmlDocument();
| doc.LoadXml(Request.InputStream.ToString());
| doc.Save("C:\\Inetpub\\wwwroot\\RX2\\test.xml");
|
| When I debug into the code, Request.InputStream.ToString() returns
| "System.Web.HttpInputStream".
|
| I've debugged intot he code, and looked at the Request object. Nowhere
| does it have the XML.
|
|
|
|
 
S

Steven Cheng[MSFT]

Hi RHPT,

Have you got any progress on this issue or does the suggestion in my
previous messages helps. If there are anything else we can help, please
feel free to pos there. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 47208013
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Fri, 19 Aug 2005 02:24:34 GMT
| Subject: Re: Retrieving values from HTTP POST
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 97
| Path: TK2MSFTNGXA01.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:119027
| NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
|
| Hi RHPT,
|
| You means the XML content in your request are coming from the clientside
| input in a TextBox? If so, I don't think we can directly use the
| Request.InputStream to load the Xml content since the whole http
message's
| contentType is event not pure XML. We can load XmlDocument directly from
| the Request.InputStream only if the pure request message is "text/xml"
| contentType. For example, we can send XML content to a remote page
through
| the following code:
|
| ==============
| static void Post_XML()
| {
| string url = "http://remoteserver/app/remotepage.aspx";
|
| HttpWebRequest webreq = WebRequest.Create(url) as HttpWebRequest;
|
| webreq.Method = "POST";
| webreq.ContentType = "text/xml";
|
|
| XmlDocument doc = new XmlDocument();
| doc.LoadXml(
| @"<?xml version='1.0' ?>
| <root>
| <items>
| <item id='1'>item1</item>
| <item id='2'>item2</item>
| <item id='3'>item3</item>
| <item id='4'>item4</item>
| </items>
| </root>
| "
| );
|
| StreamWriter sw = new StreamWriter(webreq.GetRequestStream(),
| System.Text.Encoding.UTF8);
| sw.Write(doc.OuterXml);
| sw.Close();
|
| HttpWebResponse webrep = webreq.GetResponse() as HttpWebResponse;
| StreamReader sr = new
| StreamReader(webrep.GetResponseStream(),System.Text.Encoding.UTF8);
|
| Console.WriteLine(sr.ReadToEnd());
| sr.Close();
| webrep.Close();
|
| }
| ===========================
|
| Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
| --------------------
| | NNTP-Posting-Date: Thu, 18 Aug 2005 20:35:01 -0500
| | From: RHPT <[email protected]>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet
| | Subject: Re: Retrieving values from HTTP POST
| | Date: Thu, 18 Aug 2005 20:34:57 -0500
| | Message-ID: <[email protected]>
| | References: <[email protected]>
| <[email protected]>
| <[email protected]>
| <[email protected]>
| | X-Newsreader: Forte Agent 2.0/32.652
| | MIME-Version: 1.0
| | Content-Type: text/plain; charset=us-ascii
| | Content-Transfer-Encoding: 7bit
| | Lines: 21
| | NNTP-Posting-Host: 69.137.71.225
| | X-Trace:
|
sv3-tqkcQ2XW4EE9xw18jiJjXuANveHVgbfoc05N5egU8S4IaI6IrVyNbV/YEaLlsoyLoC+VJvUw
|
0DGWkFd!QIWeCmsrkJS0gql7mj+eeXKYNjlQyKqWTL1VrYRwVeYo45DLITxDL3JenJbf86cv9ddq
| MSJcW+yb!NbDf2F9l68qFqzNhrw==
| | X-Complaints-To: (e-mail address removed)
| | X-DMCA-Complaints-To: (e-mail address removed)
| | X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
| | X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
| complaint properly
| | X-Postfilter: 1.3.32
| | Path:
|
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
|
ne.de!border2.nntp.dca.giganews.com!nntp.giganews.com!border1.nntp.dca.gigan
|
ews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POST
| ED!not-for-mail
| | Xref: TK2MSFTNGXA01.phx.gbl
| microsoft.public.dotnet.framework.aspnet:119021
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| |
| | Here's something strange:
| |
| | On default.aspx, I have a textbox that I fill with the following XML
| | string and then submit:
| |
| |
|
<myFields><txt_name>adsf</txt_name><gender_buttons>Male</gender_buttons><bir
| thday>2005-08-17</birthday><spam_me>true</spam_me></myFields>
| |
| | On my code behind page, I have the following code:
| |
| | XmlDocument doc = new XmlDocument();
| | doc.LoadXml(Request.InputStream.ToString());
| | doc.Save("C:\\Inetpub\\wwwroot\\RX2\\test.xml");
| |
| | When I debug into the code, Request.InputStream.ToString() returns
| | "System.Web.HttpInputStream".
| |
| | I've debugged intot he code, and looked at the Request object. Nowhere
| | does it have the XML.
| |
| |
| |
| |
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top