I am using a client-server scenario. The client communicates with the server(which is a servlet) using url connection. here is the code that i am using.
URL url = new URL("http://localhost:8080/hello");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());//1st out put stream
out.writeObject(pk);
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());//1st instream
PublicKey spk=(PublicKey)in.readObject();
in.close();
ObjectOutputStream out1=new ObjectOutputStream(connection.getOutputStream());//2nd out put stream
out1.writeObject(str1);
out1.flush();
out1.close();
ObjectInputStream in1 = new ObjectInputStream(connection.getInputStream());
String rstr3=(String)in1.readObject();
//processing
in1.close();
But i am getting an exception called:
java.net.ProtocolException:Cannot write output after reading input.
Where am I going wrong?
Instances of URLConnection are not reusable: you must use a different instance for each connection to a resource. The following code works properly(open a new urlconnection object)