JDK-4382944 : Can't write to same URLConnection after the writing and reading to/from it once
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.1.7
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2000-10-25
  • Updated: 2000-10-26
  • Resolved: 2000-10-26
Description

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) 
======================================================================

Comments
WORK AROUND Name: mt13159 Date: 10/25/2000 No workaround. ======================================================================
11-06-2004

EVALUATION The specification for java.net.HttpURLConnection propertly documents that the URLConnection can only be used to make a single request :- "Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances." Based on this I am closing this as "not a bug" alan.bateman@ireland 2000-10-26
26-10-2000