2017-09-12 4 views
-1

HttpUrlConnection에 문제가 있습니다.HttpUrlConnection이 'unauthorized'또는 'already connected'입니다.

인터넷에서 파일을 다운로드하고 싶습니다.
처음으로 완벽하게 작동합니다.
하지만 다시 시도 할 때 "이미 연결됨"또는 "연결 진행 중"이라는 예외가 있습니다.

Exception in thread "main" java.lang.IllegalStateException: connect in 
progress 

내가 검색 한 결과 con.setDoOutput(true);과 같은 일부 항목을 제거해야합니다. 나는 그것을했고 내 프로그램을 재부팅했다. 이제 예외가 발생했습니다 : 401 unauthorized.

나는 무엇을 해야할지 모르겠다. 내 코드 :

public boolean creerSession(String endpoint) throws IOException, JSONException, InterruptedException { 
    // Ouverture de la connexion 
    String url = new StringBuilder(endpoint).append("/auth").toString(); 
    URL obj = new URL(url); 
    validerAll(); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

    // Définition des Request header 
    String urlParameters = new StringBuilder("username=").append(this.login).append("&password=").append(this.password).toString();   
    con.setRequestMethod("POST"); 
    con.getRequestMethod(); 

    // Envoi de la requete en POST 
    con.setDoOutput(true); 
    DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
    wr.writeBytes(urlParameters); 
    wr.flush(); 
    wr.close(); 

아무 생각하세요?

+0

'HttpURLConnection con' 객체를 다 끝냈 으면'disconnect()'를 사용합니까? 그런 다음 각 요청마다 다시'(HttpURLConnection) obj.openConnection()'을 호출하여 새 인스턴스를 만들어야합니다. –

+1

스택 추적은 두 가지 예외 중 하나입니다. – EJP

+0

"401 unauthorized"귀하의 요청이 승인되지 않았 음을 나타내는 웹 서비스에서받은 응답이라고 생각합니다. 잘못된/누락 된 사용자/암호 조합과 같은 권한을 얻으려는 시도에 문제가있을 수 있습니다. – Quintium

답변

0

@Emanuele Giona 맞아요, disconnect()가 누락되었습니다.하지만 시도해도 오류가 발생합니다. 그 @Quitium 내가 설명 무엇 : 내 코드

con.setRequestMethod("POST"); 
con.getRequestMethod(); 
con.setDoOutput(true); 

주석을 제거하면 내가

이 가

스택 추적 (괜찮아 처음 몇 분 후, 진행중인 연결에 문제가 돌아 오면 인증되지 않은 예외가 ...)

Connected to the target VM, address: '127.0.0.1:61323', transport: 'socket' ticket de la session : XIZZAxWt36k0+lrEiVDKO6ijKitlwdwL1kx1EG1tiGGV3fJ3gw/In9zkBhTRjuTVWupmAh3xPQ1jRkByj50uRl0sn1vDdzE+gRiNHFIiF4TOmBQXwSkIPA== Exception in thread "main" java.lang.IllegalStateException: connect in progress at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:517) at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:374) at GedWSTesting.downloadFile(GedWSTesting.java:60) at GedWSTesting.main(GedWSTesting.java:32) Disconnected from the target VM, address: '127.0.0.1:61323', transport: 'socket'

Process finished with exit code 1

내 코드 (행 57 → 64)

// open the connection 
    HttpURLConnection con = (HttpURLConnection) downloadUrl.openConnection(); 
    con.setRequestProperty("Cookie", "LLCookie=" + mainSession.getTicket() + "; path=/livelink/; secure"); 
    con.setRequestMethod("HEAD"); 
    con.setInstanceFollowRedirects(false); 
    con.setReadTimeout(50000); 
    con.setConnectTimeout(10000); 
    con.connect(); 
+0

안녕하십니까, 너무 늦게 답변드립니다. 문제는 disconnect()입니다. – Quezac

관련 문제