2 Replies Latest reply: Oct 10, 2012 2:05 AM by gimbal2 RSS

    Java 7 - URLConnection > cannot writing to server

    963691
      Hi,

      We have a distributed system with http messaging: a client with Swing GUI and a servlet on Tomcat 6.
      I have problem send http-post message in Java 7.

      Here is a test, which works with Java 6 but not with Java 7.

      <pre>
      public static void main(String[] args) {
      URLConnection connection = null;
      OutputStream out = null;
      InputStream in = null;
      ByteArrayInputStream bain = null;
      try {
      URL url = new URL("http://localhost:8084/app/server");
      connection = url.openConnection();
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.setDefaultUseCaches(false);
      connection.setAllowUserInteraction(false);
      connection.setUseCaches(false);
      connection.setRequestProperty("Content-Type", "application/octet-stream");
      out = connection.getOutputStream();
      PrintWriter pw = new PrintWriter(out);
      pw.print("WHY?");
      pw.flush();
      pw.close();
      in = connection.getInputStream();
      byte[] bin = IOUtils.InputStream2ByteArray(in);
      System.out.println(((HttpURLConnection)connection).getResponseMessage());
      System.out.println(new String(bin));
      } catch (Exception ex) {
      Logger.getLogger(App.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
      }
      }
      </pre>

      If I compile with Java 7 and run the app, I get a IOException:
      <pre>
      SEVERE: Error writing to server
      java.io.IOException: Error writing to server
           at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:622)
           at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:634)
           at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1317)
           at fi.jamix.java7tester.App.main(App.java:43)
      </pre>

      Is there significant change between Java 6 and Java 7 with HttpURLConnection?
      I monitoring Tomcat HTTP-requests in Netbeans, and Tomcat does not get the request. The error says, writeRequest fails.
      Why? Any suggestion to find the reason?

      Edited by: belaborbely on 7.10.2012 16:39