2013-04-09 19 views
1

Я пытаюсь сделать https-соединение с URL-адресом. Я получаю это исключениеИсключение: уже подключено, HttpsUrlConnection

Exception: Уже Connected

Я не знаю, как мы создаем безопасное соединение через 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

Вы получаете заголовок Set-Cookie, прежде чем есть соединение. Не имеет никакого смысла. Если вы хотите, чтобы этот заголовок был установлен в запросе, просто установите его. getHeader() получает заголовки от ответа .

Смежные вопросы