HTTP Post Question

  • Thread starter Thread starter Matt
  • Start date Start date
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
 
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,
 
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
 
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.
 
Back
Top