wood burning stoves
The moose likes Sockets and Internet Protocols and the fly likes Http Post with XML example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Http Post with XML example" Watch "Http Post with XML example" New topic

Http Post with XML example

Richard Butterwood
Ranch Hand

Joined: Nov 07, 2005
Posts: 45
I am trying to a pass an xml file to a web server with http POST. Could someone provide me an example of this please. Below is what I have got so far. It connects to the webserver, but does not issue the correct POST informaton.

import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;

public class WebserverConnect {

private static String defaultServer
= "http://myipaddress:myport/";

public static void main(String[] args) {

/* if (args.length == 0) {
"Usage: java FibonacciXMLRPCClient index serverURL");

String index = args[0];

String server;
if (args.length <= 1) server = defaultServer;
else server = args[1]; */

String server;
String index = "0";
server = defaultServer;
ArrayList fileList;

try {
URL u = new URL(server);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
OutputStream out = connection.getOutputStream();
OutputStreamWriter wout = new OutputStreamWriter(out, "UTF-8");

wout.write("<request name=\"ValidateCustomer\" key=\"[eStoreFr]\" password=\"estore\" company=01>");


InputStream in = connection.getInputStream();
int c;
while ((c = in.read()) != -1) System.out.write(c);
catch (IOException e) {

} // end main

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

What kind of error does the server give you? Any useful messages?

I'm looking at some of our code that does the same thing. We set a few other properties such as accept=text/xml, Content-Type=text/xml and Content-length. We write right to the stream with no StreamWriter and we flush() but don't close() until after we've read the response. Let me know if any of those differences seem important to you.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Richard Butterwood
Ranch Hand

Joined: Nov 07, 2005
Posts: 45
Hi Stan,

Thanks for the reply. I figured it out myself. I need to use "setRequestProperty" to override the standard MIME headers.
Abu Aaminah

Joined: Nov 16, 2005
Posts: 1
What would be the code to do HTTP Post a string of data without any xml data (i.e. param=value&param=value)?
[ November 16, 2005: Message edited by: Abu Aaminah ]

Techie at the TM4B <a href="http://htp://www.tm4b.com" target="_blank" rel="nofollow">Bulk SMS Gateway</a>.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

It's not much different. Just write the key value pairs to the connection's output stream:

I'd usually jump on somebody for providing complete answers instead of hints in this forum, but there was so much code already here that a full example seemed to fit right in.
Don't get me started about those stupid light bulbs.
subject: Http Post with XML example
It's not a secret anymore!