This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Http Post with XML example

 
Richard Butterwood
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • 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

}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Abu Aaminah
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic