title: Exception: Already Connected, HttpsUrlConnection
body: <p>i am trying to do a https connection to a URL. I am getting this exception </p>
  
  <blockquote>
    <p>Exception: Already Connected</p>
  </blockquote>
  
  <p>I am not sure how do we create the secure connection through java. Can anyone help me with this? </p>
  
  <p>Here is my code.</p>
  
  <pre><code>public static String getCon() {
      String result="",cookie="";
      System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
      java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
  try {
  URL url= new URL("https://jazz.net/");
  HttpsURLConnection connection= (HttpsURLConnection) url.openConnection();
  String cookieHeader = connection.getHeaderField("set-cookie");
  if (cookieHeader != null) {
      int index = cookieHeader.indexOf(";");
      if (index &gt;= 0)
          cookie = cookieHeader.substring(0, index);
      connection.setRequestProperty("Cookie", cookie);
  }connection.setDoInput(true); 
  connection.setDoOutput(true);
  connection.setRequestMethod("POST"); 
  connection.setFollowRedirects(false);
  
  connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  connection.setRequestProperty("Accept", "application/xml");
  connection.setRequestProperty("User-Agent", "yes");
  DataInputStream input = new DataInputStream( connection.getInputStream() ); 
  
  StringBuffer buf= new StringBuffer();
  // read in each character until end-of-stream is detected 
  for( int c = input.read(); c != -1; c = input.read() ) 
      buf.append(c);
  input.close(); 
  result=buf.toString();
  } catch (Exception e) {
  // TODO Auto-generated catch block
  return result+" exception "+e.getMessage();
   }
  
  
      return result;
  }
  </code></pre>