1

我正在尝试与 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;
}
4

1 回答 1

0

您甚至在连接之前就获得了 Set-Cookie 标头。没有任何意义。如果您想在请求中设置该标头,只需设置它。getHeader() 从响应中获取标头。

于 2013-04-09T22:50:45.450 回答