com.sun.xml.internal.ws.client.ClientTransportException:HTTP传输错误:java.net.SocketException:连接重置

时间:2019-03-22 04:46:38

标签: java soap

使用JDK 1.6用Java开发的客户端。我在Java代码中使用了API。每当我从soapui或JDK 1.7中访问此API时,它都可以正常工作,但是当我尝试使用JDK 1.6时访问此API时,它将返回错误。 com.sun.xml.internal.ws.client.ClientTransportException:HTTP传输错误:java.net.SocketException:连接重置。

我尝试通过使用WSDL和HTTPSURLConnection开发客户端,同时使用两种机制,都遇到相同的错误。代码似乎没有错。我无法找到解决方法。

Wireshark结果: 当我从JDK 1.7运行jar时,可以在Wireshark中看到结果,协议为TSLv1,但是当我尝试从1.6运行jar时,协议已更改为SSLv2。 是否可以在代码或我们调用jar的系统上更改协议?

这是我的代码:

 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;
}

1 个答案:

答案 0 :(得分:0)

在SSLContext中,您可以设置自己的SSlContext TSLv1或SSLv2,然后使用受信任的证书调用sslContext.init。然后,将其作为DefaultSSLSocketFactory添加到您的HttpsURLConnection中。

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);