HTTP Post Question

M

Matt

Hi,

I am trying to to create a HTTP Request that posts XML that mimics the html
form below.

<form name="Form1" method="post" action="http://test/xml.aspx" id="Form1">
<textarea name="OrderXml" id="OrderXml"
style="height:342px;width:550px;Z-INDEX: 104; LEFT: 65px; POSITION:
absolute; TOP: 84px" rows="1" cols="20"></textarea>
<p>
<input type=submit name='Submit Order' value='Submit Order'>
</form>

I can get the code below to work. Any ideas?

HttpWebRequest httpRequest =
(HttpWebRequest)WebRequest.Create(http://test/xml.aspx);
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml";
XmlTextWriter xmlWriter = new XmlTextWriter(httpRequest.GetRequestStream(),
System.Text.Encoding.UTF8);
xmlWriter.WriteStartDocument();
//.. Write XML
xmlWriter.WriteEndDocument();
xmlWriter.Close();
HttpWebResponse httpResponse = (HttpWebResponse)
httpRequest.GetResponse();
//.. store XML
 
J

Joerg Jooss

Thus wrote Matt,
Hi,

I am trying to to create a HTTP Request that posts XML that mimics the
html form below.

<form name="Form1" method="post" action="http://test/xml.aspx"
id="Form1">
<textarea name="OrderXml" id="OrderXml"
style="height:342px;width:550px;Z-INDEX: 104; LEFT: 65px; POSITION:
absolute; TOP: 84px" rows="1" cols="20"></textarea>
<p>
<input type=submit name='Submit Order' value='Submit Order'>
</form>
I can get the code below to work. Any ideas?

HttpWebRequest httpRequest =
(HttpWebRequest)WebRequest.Create(http://test/xml.aspx);
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml";
XmlTextWriter xmlWriter = new
XmlTextWriter(httpRequest.GetRequestStream(),
System.Text.Encoding.UTF8);
xmlWriter.WriteStartDocument();
//.. Write XML
xmlWriter.WriteEndDocument();
xmlWriter.Close();
HttpWebResponse httpResponse = (HttpWebResponse)
httpRequest.GetResponse();
//.. store XML


Your code does something rather different from what the HTML form does.

The HTML form will send a HTTP message body like this:
OrderXml=<contentOfOrderXml>&Submit+Order=Submit+Order

Your code posts a raw XML document in the HTTP message body:
<?xml version="1.0" ?><foo><bar>xyz</bar></foo>

Also note that Content-Type for an HTML form is application/x-www-form-urlencoded,
not text/xml.

Therefore
- change your Content-Type
- make sure to write URL encoded key/value pairs to the request stream, with
the keys being the form field names and the values being the form field values

Note that WebClient.UploadValues() already implements all of the above ;-)

Cheers,
 
M

Matt

I appreciate the response. I had a few questions:

1) How can I get NameValueCollection for WebClient.UploadValues() represent
a tiered xml document?
2) I did not follow what you mean by: make sure to write URL encoded
key/value pairs to the request stream, with
the keys being the form field names and the values being the form field
values Can you explain this?

Thanks
 
M

Matt

Disregard my last post I figured it out. I just add the xml string as in
NameValueCollection with a key "OrderXML". It works great and thanks for
the help.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top