• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Http Post with XML example

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {
System.out.println(
"Usage: java FibonacciXMLRPCClient index serverURL");
return;
}

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;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
OutputStream out = connection.getOutputStream();
OutputStreamWriter wout = new OutputStreamWriter(out, "UTF-8");

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

wout.flush();
out.close();

InputStream in = connection.getInputStream();
int c;
while ((c = in.read()) != -1) System.out.write(c);
System.out.println();
in.close();
out.close();
connection.disconnect();
}
catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}

} // end main

}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Richard Butterwood
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stan,

Thanks for the reply. I figured it out myself. I need to use "setRequestProperty" to override the standard MIME headers.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic