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 に答える