例外:已经连接,HttpsUrlConnection

时间:2013-04-09 10:28:41

标签: java https httpsurlconnection

我正在尝试与URL建立https连接。我得到了这个例外

  

例外:已经连接

我不确定如何通过java创建安全连接。谁能帮我这个?

这是我的代码。

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 >= 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;
}

1 个答案:

答案 0 :(得分:0)

在连接之前,您将获得Set-Cookie标头。没有任何意义。如果您希望在请求中设置标头,请设置它。 getHeader()从响应中获取标题。