473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help - how to POST in XML format

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
3 2129
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Adam Steinberger | last post by:
Hey, I'm currently working on an address book using classes that will take and email address and a name and save it in a certain format in a text file. Could you help me! I'm sure my code should...
14
by: Michael Barrido | last post by:
I have this for example: Dim iSeconds as int32 = 3600 '3600 seconds is one hour How do i convert it to "01:00:00" ? Please help. Thanks in advance!
32
by: robert d via AccessMonster.com | last post by:
I'm looking at converting DAO to ADO in my app. All of my DAO connections are of the following structure: Dim wsName As DAO.Workspace Dim dbName As DAO.Database Dim rsName As DAO.Recordset ...
22
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
5
by: bhatia | last post by:
Hello all, I am implementing JPEG-LS for my very specific use, but I am not being able to fine proper specifications for it's format. Can anybody please help me out and come forward with some...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
14
by: ghostwalker | last post by:
Hi I have an HTML form with a textarea on it. When submitted (using 'get' not 'post') this forms action php file simply does this to retrieve the values: $message = $_GET; Now it all works...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
2
by: helraizer1 | last post by:
Hi folks, I have a file for my chatbox called data.line, which the posts are in the layout CHATBOXTEXT 7 username=helraizer 1202416953
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.