By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

Help - how to POST in XML format

P: n/a
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 !!!!!
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/de...ethodtopic.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.GetRequestSt ream())
streamWriter.Write(stringPost)
streamWriter.Close()

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

C# sample:
http://authors.aspalliance.com/steve...netscrape2.asp
- Jon
http://weblogs.asp.net/jgalloway

Nov 18 '05 #2

P: n/a
Tom wrote:
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,

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de
Nov 18 '05 #3

P: n/a
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/de...ethodtopic.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.GetRequestSt ream())
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,

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.