Can't reset method: already connected

风流意气都作罢 提交于 2019-12-06 14:54:18

问题


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.

来源:https://stackoverflow.com/questions/34447190/cant-reset-method-already-connected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!