title: HttpURLConnection is Connection reset or Broken pipe
body: <p>I am trying to upload byte array to http server using post method from android. But i am getting always connection reset or Broken pipe error. Could pls help me understand where i am doing wrong.</p>
  
  <pre><code>int bufferBytes =  131072;  // 128 KB
  Random r = new Random( System.currentTimeMillis() );
  int fileName  = 10000 + r.nextInt(20000);
  
  String urlParameters  = ""+ StringUtils.leftPad("abcdefgh", bufferBytes, "abcdefgh");
  byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
  
  String CRLF = "\r\n";
  String boundary = "-----------------------******";
  
  OutputStream out = null;
  HttpURLConnection urlConnection = null;
  PrintWriter writer = null;
  try {
  
  
  URL url = new URL("host");
  urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setRequestMethod("POST");
  urlConnection.setConnectTimeout(10*1000) ; // 15 Sec time out
  System.setProperty("http.keepAlive", "false");
  
  urlConnection.setChunkedStreamingMode(1024);
  //urlConnection.setRequestProperty("Keep-Alive", "false");
  urlConnection.setRequestProperty("connection", "close");
  urlConnection.setUseCaches (false);
  
  urlConnection.setDoOutput(true);
  urlConnection.setRequestProperty("Accept", "text/xml,application/x ml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
  urlConnection.setConnectTimeout(15*1000) ; // 15 Sec time out
  urlConnection.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
  urlConnection.setRequestProperty("Accept-Encoding", "identity");
  urlConnection.setRequestProperty("Transfer-Encoding", "chunked") ;
  
  urlConnection.connect();
  out = urlConnection.getOutputStream();
  writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8"), true);
  
  // Send normal param.
  writer.append("100000").append(CRLF);
  writer.append(boundary).append(CRLF);
  writer.append("Content-Disposition: form-data; name=\"success_action_status\"").append(CRLF);
  writer.append("201").append(CRLF);
  writer.append(boundary).append(CRLF);
  writer.append("Content-Disposition: form-data; name=\"Content-Type\"").append(CRLF);
  writer.append("image/jpeg").append(CRLF);
  writer.append(boundary).append(CRLF);
  writer.append("Content-Disposition: form-data; name=\"x-amz-acl\"").append(CRLF);
  writer.append("public-read").append(CRLF);
  writer.append(boundary).append(CRLF);
  writer.append("Content-Disposition: form-data; name=\"key\"").append(CRLF);
  writer.append("images/237615.jpg").append(CRLF);
  writer.append(boundary).append(CRLF);
  writer.append("Content-Disposition: form-data; name=\"FileName\"").append(CRLF);
  writer.append("ul+"+fileName+".jpg").append(CRLF);
  writer.append(boundary).append(CRLF);
  
  writer.append("Content-Disposition: form-data; name=\"file\"; filename=ul" +fileName).append(CRLF);
  writer.append("Content-Type: image/jpeg").append(CRLF); // Text file itself must be saved in this charset!
  writer.append(CRLF).flush();
  out.flush(); // Important before continuing with writer!
  writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
  
  
  
  long startTime = System.currentTimeMillis();
  while (true){
      out.write(postData);
      out.flush();
      if(System.currentTimeMillis() - startTime &gt; 10000)
      break;
  }
  
  Log.d("Upload" , " Upload is success");
  
  } catch (Exception e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
  }finally {
      try {
          if(writer != null)
              writer.close();
          if(out != null)
          out.close();
          if(urlConnection != null)
              urlConnection.disconnect();
      }catch (Exception e){
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>If i comment the below line i am getting the java.net.SocketException:  Connection reset else i am getting  the java.net.SocketException: Broken pipe</p>
  
  <pre><code>System.setProperty("http.keepAlive", "false"); 
  </code></pre>