0

I have seen this error will throw if called getInputStream before, but I have not in my code and I have checked the connected property is false, but still show this error. the code is as follow:

private void testConnect() throws Exception{
    HttpURLConnection con = null;
    OutputStream httpsend = null;
    String url = "https://xxxxx/goldapi/rs/checkPaymentService";

    TrustManager[] trustall = new TrustManager[] { 
        new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
                return new X509Certificate[0];
            }
            public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {}

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
        } 
    }; 
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustall, new SecureRandom()); 
    HostnameVerifier allHostsValid = new HostnameVerifier() {
         @Override
         public boolean verify(String hostname, SSLSession session) {
             System.out.println("URL Host: " + hostname + " vs. " + session.getPeerHost());
             return true;
         }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    con = (HttpsURLConnection)(new URL(url).openConnection());
    con.setConnectTimeout(100000);

    // Set HTTP parameters for SOAP call
    //con.disconnect(); // test
    con.setAllowUserInteraction(true);
    con.setRequestMethod("POST");

    con.setDoOutput(true);
    con.setDoInput(true);
    con.setUseCaches(false);
    byte[] b = "test".getBytes();
    con.setRequestProperty( "Content-Length", String.valueOf( b.length));
    con.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  //Send request to server
    httpsend = con.getOutputStream();
    httpsend.write(b);
    httpsend.flush();
    try{
        httpsend.close();
    }catch(Exception e){
        e.printStackTrace();
    }

}

it will throws Exception

Exception in thread "main" java.net.ProtocolException: Can't reset method:
at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:320)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod (HttpsURLConnectionImpl.java:354)
at Test.PSGConnectTest.testConnect(PSGConnectTest.java:62)
at Test.PSGConnectTest.main(PSGConnectTest.java:24)

already connectedI found that I need to explicit write

con.disconnect();

to prevent this error, but then another strange exception throw when running

httpsend = con.getOutputStream();

this statement will throws exception as follow

java.net.ProtocolException: Cannot write output after reading input.

but I have not reading any input (no getInputStream before), so what is the problem?

Update: I found this error only occur when I use debug mode in eclipse, no this error if run in eclipse run mode.

user1169587
  • 1,104
  • 2
  • 17
  • 34
  • Can you paste Exception stack trace as well? – Ankit Dec 24 '15 at 05:45
  • stacktrace added and additional finding is added, seem related with eclipse debug mode – user1169587 Dec 24 '15 at 06:06
  • 1
    Someone else also had the same problem - http://stackoverflow.com/a/28485029/810176 – Ankit Dec 24 '15 at 06:22
  • yes, but anyone know the reason? why cannot put a breakpoint on con.setRequestMethod("POST");? – user1169587 Dec 24 '15 at 06:50
  • No idea, but if you wish to debug it then install some http monitor on your system and then see what is the actual time of http request. Ideally, in your case, it should be after con.getOutputStream() line. – Ankit Dec 24 '15 at 07:15
  • I can try and replicate the issue if you can share your paymentservice URL – Ankit Dec 24 '15 at 07:23

0 Answers0