简体   繁体   中英

com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: java.net.SocketException: Connection reset

A client developed in Java using JDK 1.6. I am consuming API in the java code. Whenever I hit this API from soapui or from JDK 1.7 it is working perfectly fine but when I tried to hit this API using JDK 1.6, it is returning the error. com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: java.net.SocketException: Connection reset.

I have tried by developing client using WSDL and using HTTPSURLConnection, with both mechanism, I am getting the same error. It seems there is nothing wrong with the code. I am unable to find out the way for the resolution.

Wireshark Result: When I ran the jar from JDK 1.7, I can see the result in Wireshark, the protocol is TSLv1 but when I tried to run the jar from 1.6, the protocol has been changed to SSLv2. Is it possible to change protocol in the code or on the system where we are calling jar?

Here is my code:

 public String myFun(String sender) throws IOException, 
 NoSuchAlgorithmException, KeyManagementException{


    SSLContext sslContext = SSLContext.getInstance("SSL");
    TrustManager[] trustManager = getTrustManager();
    sslContext.init(null, trustManager, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

    try{
            String inquiryRequest = inquiryRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v5=\"http://xxxxxxx\">\n" 
                    +"<soapenv:Header>\n" 
                    +"</soapenv:Header>\n" 
                    +"<soapenv:Body>\n" 
                    +"<v5:single.smsReq>\n" 
                    +"<sender>"+sender+"</sender>\n" 
                    +"</v5:single.smsReq>\n" 
                    +"</soapenv:Body>\n" 
                    +"</soapenv:Envelope>";

            URL url =new URL ("https://xxxx:xx/xx");
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

            conn.setRequestMethod("POST");
            conn.setRequestProperty("content-type","application/xml");
            conn.setRequestProperty("Authorization","Basic xxx");
            conn.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
                wr.writeBytes(inquiryRequest);
                wr.flush();
                wr.close();

            BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                }
                in.close();
                conn.disconnect();

            return response.toString();

    }catch(Exception e){
        return null;
    } 

}
private TrustManager[] getTrustManager() {
    TrustManager[] certs = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(X509Certificate[] certs, String t) {
        }
        public void checkServerTrusted(X509Certificate[] certs, String t) {
        }
    }
    };
    return certs;
}

In SSLContext you can setup your own SSlContext TSLv1 or SSLv2 then call sslContext.init with trusted certificates. And, add it to your HttpsURLConnection as the DefaultSSLSocketFactory.

System.setProperty ("jsse.enableSNIExtension", "false");

SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager[] trustManager = getTrustManager();
sslContext.init(null, trustManager, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

HostnameVerifier hostNameVerifier = new HostnameVerifier() {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};

HttpsURLConnection.setDefaultHostnameVerifier(hostNameVerifier);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM