title: Android httpURLConnection already running
body: <p>I've got an app where in I was calling an httpURLConnection with the requestproperty connection "stay-alive".
  Because I didn't call the HttpURLConnection.disconnect() when my app was crashing, he didn't make it to the disconnect call because I didn't put it in the finally method. </p>
  
  <h2>Problem</h2>
  
  <p>When I rerun my app, I see that my url.openConnection() already see the old HttpUrlConnection and take that back out of the pool. Problem now is that I can't simply kill the already existed connection.</p>
  
  <p>After some research on the web I found an old bug report (<a href="https://code.google.com/p/android/issues/detail?id=2939" rel="nofollow">https://code.google.com/p/android/issues/detail?id=2939</a>)
  I already tried different approaches, but after the url.openConnection() calling httpURLConnection.disconnect() didn't do anthing. Reinstall app, reboot pc and tablet also weren't effective.</p>
  
  <h2>Known exceptions</h2>
  
  <ul>
  <li>java.lang.IllegalStateException: Already connected</li>
  </ul>
  
  <h2>Question</h2>
  
  <p>So my question is: How can I kill the existed HttpURLConnection? </p>
  
  <p>Here a brief example of the <strong>wrong</strong> used code:</p>
  
  <pre><code>try{
          File file = new File("resources/test.png");
          FileInputStream fileInputStream = new FileInputStream(file);
  
          URL url = new URL(urlRequest);
          HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
  
          //Get Cookie from session
          String key = MyHttpClient.sharedInstance().getCookieStore().getCookies().get(0).getName();
          String value = MyHttpClient.sharedInstance().getCookieStore().getCookies().get(0).getValue();
  
          //allow inputs &amp; outputs
          connection.setDoInput(true);
          connection.setDoOutput(true);
          connection.setUseCaches(false);
  
          //Enable GET method
          connection.setRequestMethod("GET");
          connection.setRequestProperty("Connection", "Keep-Alive");
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setRequestProperty("Accept", "application/json");
          connection.setRequestProperty("User-Agent", "MyAgent");
          connection.setRequestProperty("Cookie", key+"="+value);
  
          //Call with stream fields
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary =  "*****";
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1*1024*1024;
  
  
          //Call with stream
          DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
          /*outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""+ Configuration.userTempImage.getPath()+ "\""+lineEnd);
          outputStream.writeBytes(lineEnd);*/
  
          bytesAvailable = fileInputStream.available();
          bufferSize = Math.min(bytesAvailable, maxBufferSize);
          buffer = new byte[bufferSize];
  
          //Read file
          bytesRead = fileInputStream.read(buffer, 0 , bufferSize);
  
          while(bytesRead &gt; 0){
              outputStream.write(buffer, 0, bufferSize);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
          }
  
          /*outputStream.writeBytes(lineEnd);
          outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);*/
  
  
          // Responses from the server (code and message)
          int serverResponseCode = connection.getResponseCode();
          String serverResponseMessage = connection.getResponseMessage();
  
          BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
          String jsonText = readAll(rd);
  
          fileInputStream.close();
          outputStream.flush();
          outputStream.close();
  
          System.out.print(serverResponseCode + " - " + serverResponseMessage+" -&gt; ");
      } catch (Exception ex){
          System.out.print(ex.getMessage());
      }
  </code></pre>
  
  <p>And this is my <strong>updated</strong> code</p>
  
  <pre><code>private static String getImagePath(String urlRequest){
      System.setProperty("http.keepAlive", "false");
  
      // ?
      FileOutputStream fos = null;
      // The connection to the server
      HttpURLConnection connection = null;
      // The data steam for sending the file to the server
      DataOutputStream outputStream = null;
      // A file stream to read the local file.
      FileInputStream fileInputStream = null;
      // ?
      ByteArrayOutputStream stream = null;
      // Cropped image file
      File croppedImage = null;
  
      try{
          try{
              stream = new ByteArrayOutputStream();
              Configuration.userTempBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
              byte[] byteArray = stream.toByteArray();
              // Close the output-stream
              if(stream != null) { stream.close(); }
  
              croppedImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");
  
              if(croppedImage.exists()){
                  croppedImage.delete();
              }
  
              fos = new FileOutputStream(croppedImage);
              fos.write(byteArray);
          } catch (java.io.IOException e) {
              Log.e("PictureDemo", "Exception in photoCallback", e);
          } finally {
              if(fos != null) { fos.close(); }
          }
  
          //Get Cookie from session
          String key = MyHttpClient.sharedInstance().getCookieStore().getCookies().get(0).getName();
          String value = MyHttpClient.sharedInstance().getCookieStore().getCookies().get(0).getValue();
  
          URL url = new URL(urlRequest);
          connection = (HttpURLConnection)url.openConnection();
  
          //Allow inputs &amp; outputs
          connection.setDoInput(true); // &lt;-- Exception is thrown here
          connection.setDoOutput(true);
          connection.setUseCaches(false);
  
          // Set connection properties
          connection.setRequestProperty("Cookie", key+"="+value);
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setRequestProperty("Accept", "application/json");
          connection.setRequestProperty("User-Agent", "MyAgent");
  
          // Set connection headers
          connection.setRequestMethod("GET");
          connection.setConnectTimeout(10000);
          connection.setReadTimeout(10000);
  
          //Call with stream fields
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1*1024*1024;
  
          // Create an input stream to the cropped image file
          fileInputStream = new FileInputStream(croppedImage);
          // Create an output stream to send the file to the server
          outputStream = new DataOutputStream(connection.getOutputStream());
  
          bytesAvailable = fileInputStream.available();
          bufferSize = Math.min(bytesAvailable, maxBufferSize);
          buffer = new byte[bufferSize];
  
          //Read file
          bytesRead = fileInputStream.read(buffer, 0 , bufferSize);
  
          while(bytesRead &gt; 0){
              outputStream.write(buffer, 0, bufferSize);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
          }
  
          // Be sure to send everything
          outputStream.flush();
  
          // Responses from the server (code and message)
          int serverResponseCode = connection.getResponseCode();
          String serverResponseMessage = connection.getResponseMessage();
  
  
          BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
          String jsonText = RequestHelper.readAll(rd);
  
          return jsonText;
      }catch(Exception ex){
          ex.printStackTrace();
      } finally {
          try{
              if(fileInputStream != null) { fileInputStream.close(); }
              if(outputStream != null) { outputStream.close(); }
          }catch(Exception ex){
              ex.printStackTrace();
          }finally{
              if(connection != null) { connection.disconnect(); }
          }
      }
  
      return null;
  }
  </code></pre>