| Author |
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) { 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 }
|
 |
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
Greenhorn
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¶m=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.
|
 |
 |
|
|
subject: Http Post with XML example
|
|
|