Wednesday, December 15, 2004

Sending and receiving objects over an HTTP connection

public static Object sendObject(Object obj) {
URLConnection conn = null;
Object reply = null;
try {
// open URL connection
conn = servletURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches( false );
// send object
ObjectOutputStream objOut = new ObjectOutputStream(
conn.getOutputStream() );
objOut.writeObject( obj );
objOut.flush();
objOut.close();
} catch ( IOException ex ) {
ex.printStackTrace();
return null;
}
// recieve reply
try{
ObjectInputStream objIn = new ObjectInputStream(
conn.getInputStream() );
reply = objIn.readObject();
objIn.close();
} catch ( Exception ex ) {
// it is ok if we get an exception here
// that means that there is no object being returned
if( !(ex instanceof EOFException))
ex.printStackTrace();
//System.err.println("*");
}
return reply;
}

ref: edu.upmc.opi.caBIG.caTIES.client.vr.utils.ncimetasearch.NCIMetaSearchComponent

On the servlet side :

ObjectInputStream objIn = new ObjectInputStream(req.getInputStream()); try { Object obj = objIn.readObject();

ref: edu.upmc.opi.caBIG.caTIES.server.mmtx.MMTxService

No comments: