2014-09-05 25 views
5

Tôi nhận được java.lang.IllegalStateException: Already connected ngoại lệ khi cố gắng chạy để thực hiện một HTTPS GET yêu cầu với HttpsURLConnection API.java.lang.IllegalStateException: Đã kết nối tại setDoOutput

Hãy tìm thấy bên dưới mã:

HttpsURLConnection con = null; 

    try { 
     URL obj = new URL(url); 
     con = (HttpsURLConnection) obj.openConnection(); 

     con.setRequestMethod("GET"); 
     con.setRequestProperty("Authorization", header); 
     con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); 
     String urlParameters = "schema=1.0&form=json&byBillingAccountId={EQUALS,[email protected]}"; 

     con.setDoOutput(true); 

     DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
     wr.writeBytes(urlParameters); 
     wr.flush(); 
     wr.close(); 

     int responseCode = con.getResponseCode(); 

     System.out.println("Response Code = " + responseCode); 

     BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 

     while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 
     con.disconnect(); 
     //print result 
     System.out.println("Response = " + response.toString()); 


    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if(con!=null) con.disconnect(); 
    } 

Không chắc lý do tại sao tôi nhận được đó.

Bất kỳ ý tưởng nào về cách giải quyết vấn đề này?

Stack Trace

java.lang.IllegalStateException: Already connected 
at java.net.URLConnection.setDoOutput(Unknown Source) 
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setDoOutput(Unknown Source) 
at com.comcast.ccp.xbo.testing.partnerdevices.AppTest.testReadProperties(AppTest.java:65) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 
at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:200) 
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:155) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:212) 
at org.testng.internal.Invoker.invokeMethod(Invoker.java:707) 
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
at org.testng.TestRunner.privateRun(TestRunner.java:767) 
at org.testng.TestRunner.run(TestRunner.java:617) 
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 
at org.testng.TestNG.run(TestNG.java:1057) 
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) 
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 
+0

Stack trace xin – StackFlowed

+0

thêm stack trace – user1356042

Trả lời

3

Theo this question setDoOutput được sử dụng cho PUT và POST yêu cầu (chúng chứa một request-thực thể cơ thể). Vì vậy, có xung đột với phương thức yêu cầu được đặt theo cách thủ công của bạn GET.

Bạn đang sử dụng GET để tham số url phải được đặt như trong this question và bạn có thể chỉ cần xóa setDoOutput.

Cũng Content-Type không có ý nghĩa với GET-yêu cầu (source)

+0

Thanks ... này giải quyết vấn đề này. Điều này giới thiệu một vấn đề khác cần giải quyết và đó là đặt tiêu đề Xác thực cho kết nối https này. con.setRequestProperty ("Ủy quyền", tiêu đề) dường như không đặt tiêu đề. Bất kỳ ý tưởng? – user1356042

+0

Có thể xóa tiêu đề Loại Nội dung không đúng/không cần thiết sẽ giúp? – DaniEll

+0

đã xóa nhưng máy chủ trả về 401 có nghĩa là có vấn đề xác thực và tiêu đề không được đặt. – user1356042

Các vấn đề liên quan