title: Java, HttpUrlConnection. Problem with getResponseCode()
body: <p>I try to create the simplest Simplest WebServer and Client using HTTP. (Please, don't tell me to using Apache HTTPClient).</p>
  
  <p>Client: try to PUT some file to Server.</p>
  
  <pre><code>// **PUT**
  if(REQUEST.toUpperCase().equals("PUT")) {
  
    File sourceFile = new File(fileName);
  
    if(!sourceFile.canRead()) {
        System.out.println("Have not access to this file...");
        return;
    }
  
    try {
        BufferedInputStream is = new BufferedInputStream(new FileInputStream(sourceFile));
  
        URL url = new URL("http://" + HOST+":"+PORT);
        System.setProperty("http.keepAlive", "true");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
  
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("Content-Type", "Application/octet-stream");
        connection.setRequestProperty("Content-Length",Long.toString(sourceFile.length()));
        connection.addRequestProperty("Content-disposition","attachment; filename="+fileName);
  
        BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
  
        byte[] buf = new byte[sizeArr];
        int r = 1;
        while((r = is.read(buf)) &gt; 0) {
            os.write(buf, 0, r);
        }
        os.flush();
        os.close();
  
        System.out.println("Waiting for the response...");//this is written to console
  
        System.out.println(connection.getResponseCode());//HERE infinite waiting
        is.close();
  
  
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    }
  </code></pre>
  
  <p>On Server: if <code>Request == PUT</code>, then:</p>
  
  <pre><code>// **PUT**
  if (header.toUpperCase().equals("PUT")) {
      System.setProperty("http.keepAlive", "true");
      String fileName = null;
  
      if((fileName = extract(request.toUpperCase(),"FILENAME=","\n")) == null) {
          fileName = "UnknownFile.out";
      }
  
      try {
      File sourceFile = new File(fileName);
      BufferedOutputStream osFile = new BufferedOutputStream 
          (new FileOutputStream(sourceFile));
  
      byte[] locbuf = new byte[sizeArr];
      int locr = 1;
      while((locr = is.read(locbuf)) &gt; 0) {
          System.out.println("locr= "+locr);//this is written to console
          osFile.write(locbuf, 0, locr);
      }
      System.out.println("Ending to record the data to the file."); 
      //  this is NOT written to console
      osFile.flush();
      osFile.close();
      }
      catch(IOException ex) {
          os.write(CodeRequest("500 Internal Server Error").getBytes());
          os.close();
          ex.printStackTrace();
          return;
      }
  
      System.out.println("Trying to send 200 OK");
      os.write(CodeRequest("200 OK").getBytes());
      os.flush();
      os.close(); // where os = clientSocket.getOutputStream();
      }
  </code></pre>
  
  <p>Why doesn't the Client get a Response from the Server? If I interrupted the Client's infinite loop, then WebServer would correctly record data to file. But Client will never know that his file was normally uploaded to the server. If I comment out this statement on Client:</p>
  
  <pre><code>// System.out.println(connection.getResponseCode());
  </code></pre>
  
  <p>Then Client correctly exit from loop and ends. But Server doesn't even write to console this:</p>
  
  <pre><code>while((locr = is.read(locbuf)) &gt; 0) {
    System.out.println("locr= "+locr);//this is NOT written to console
    osFile.write(locbuf, 0, locr);
  }
  </code></pre>
  
  <p>Server ONLY writes this to console this:</p>
  
  <pre><code>localExcString index out of range: -1
  </code></pre>
  
  <p>without any Error message.</p>
  
  <p>What's wrong?</p>