Help - how to POST in XML format

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

Hi.
After I got some information from the user
I need to POST it in XML format to the server

NOT MY SERVER - a server at https://xxxx.xxx

and to reveive the response.

Please let me know how to do it.

THANKS !!!!!
 
Overview:
The best way is to use the HttpWebRequest object. Set Method property
to "POST" and the ContentType to "application/x-www-form-urlencoded".
Get a reference to the RequestStream using GetRequestStream and use it
to fill the form fields the remote server is expecting.

How to set the data to the form field (field name is firstone in this
sample):
http://msdn.microsoft.com/library/d...rfsystemnethttpwebrequestclassmethodtopic.asp

VB.NET sample (from http://www.paypaldev.org/topic.asp?TOPIC_ID=2391):
'read post from PayPal system and add 'cmd'
dim stringPost = Request.Form.ToString & "&cmd=_notify-validate"

dim httpWebRequest as System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create("https://www.paypal.com/cgi-bin/webscr")
httpWebRequest.Method = "POST"
httpWebRequest.ContentLength = stringPost.Length
httpWebRequest.ContentType = "application/x-www-form-urlencoded"

dim streamWriter = new
System.IO.StreamWriter(httpWebRequest.GetRequestStream())
streamWriter.Write(stringPost)
streamWriter.Close()

dim httpWebResponse as System.Net.HttpWebResponse =
httpWebRequest.GetResponse()
dim streamReader = new
System.IO.StreamReader(httpWebResponse.GetResponseStream())
dim stringResult = streamReader.ReadToEnd()
streamReader.Close()

C# sample:
http://authors.aspalliance.com/stevesmith/articles/netscrape2.asp
- Jon
http://weblogs.asp.net/jgalloway
 
Tom said:
Hi.
After I got some information from the user
I need to POST it in XML format to the server

NOT MY SERVER - a server at https://xxxx.xxx

and to reveive the response.

Please let me know how to do it.

THANKS !!!!!

Here's some sample code that should get you started:

public void PostXml(string url, string xml, Encoding encoding) {
byte[] bytes = encoding.GetBytes(xml);
PostBinary(url, bytes, "text/xml");
}

public void PostBinary(string url, byte[] bytes, string contentType) {
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = contentType;
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
}

using (HttpWebResponse response = (HttpWebResponse)
request.GetResponse()) {
if (response.StatusCode != HttpStatusCode.OK) {
string message = String.Format(
"POST failed. Received HTTP {0}",
response.StatusCode);
throw new ApplicationException(message);
}
}
}

Cheers,
 
jongalloway wrote
Overview:
The best way is to use the HttpWebRequest object. Set Method property
to "POST" and the ContentType to "application/x-www-form-urlencoded".

Not if you want to post raw XML. application/x-www-form-urlencoded applies
only to web forms, and this is not what you normally use when sending XML.

Get a reference to the RequestStream using GetRequestStream and use it
to fill the form fields the remote server is expecting.

How to set the data to the form field (field name is firstone in this
sample):
http://msdn.microsoft.com/library/d...rfsystemnethttpwebrequestclassmethodtopic.asp

VB.NET sample (from http://www.paypaldev.org/topic.asp?TOPIC_ID=2391):
'read post from PayPal system and add 'cmd'
dim stringPost = Request.Form.ToString & "&cmd=_notify-validate"

dim httpWebRequest as System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create("https://www.paypal.com/cgi-bin/webscr")
httpWebRequest.Method = "POST"
httpWebRequest.ContentLength = stringPost.Length
httpWebRequest.ContentType = "application/x-www-form-urlencoded"

dim streamWriter = new
System.IO.StreamWriter(httpWebRequest.GetRequestStream())
streamWriter.Write(stringPost)
streamWriter.Close()

And here's the #1 of all WebRequest related bugs: Setting the Content-Length
without knowing the real number of bytes in advance. This code fails if you
send non-US-ASCII characters, as such characters may take up two or more
bytes. Just forget about StreamWriters and StreamReaders here. Encode the
payload manually and you'll be fine.

Cheers,
 
Back
Top