Name: mt13159 Date: 10/25/2000 java version "1.1.8" A URLConnection is used from an applet to communicate with a servlet. The idea is to keep using the same connection to write output and read input many times. The problem seen is that after the first round of writing output and reading input, the same URLConnection cannot be used to write output any more. The exception stack that is throwing is java.net.ProtocolException: Cannot write output after reading input. at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Http URLConnection.java:358) From the description, the URLConnection cannot be used to write output after reading input. There is no reason, from application point of view, that the same URLConnection cannot be used to write and read data, as long as the other end of the URLConnection can handle it properly. There is no documentation saying it cannot be used after reading input also. The java source code that can reproduce this problem is followed. public void connect() { try { URL servletURL = new URL(location); servletConnection = servletURL.openConnection(); servletConnection.setDoOutput(true); servletConnection.setDoInput(true); servletConnection.setUseCaches (false); servletConnection.setDefaultUseCaches (false); servletConnection.setRequestProperty("Content-Type", "application/octet-stream"); } catch (Exception e) { e.printStackTrace(); } } public ObjectInputStream sendMessage(Serializable s) { try { // java.net.ProtocolException: Cannot write output after reading input // is thrown the next line ObjectOutputStream oout = new ObjectOutputStream(servletConnection.getOutputStream()); oout.writeObject(s); oout.flush(); return new ObjectInputStream(servletConnection.getInputStream()); } catch (Exception e) { e.printStackTrace(); return null; } } The connect() is called once per application and sendMessage() should be called to write data to the OutputStream. (Review ID: 110796) ======================================================================
|