2017-08-21 1 views
1

Je suis en train de publier HTTP POST via HttpClient sur un serveur avec l'authentification du client activée. Voici mon codeVous avez "Erreur Windows Sockets non reconnue: 0: échec de la récupération" lors de l'envoi du message

public class Send2Remote { 

private static String sslMode = null; 
private static String clientKeyStore = null; 
private static String clientStoreType = null; 
private static String clientStorePW = null; 

private static String trustKeyStore = null; 
private static String trustStoreType = null; 
private static String trustStorePW = null; 

public Send2Remote(String sslmode, String clientKS, String clientST, String clientTPW, 
     String trustKS, String trustST, String trustSPW) { 
    sslMode = sslmode; 
    clientKeyStore = clientKS; 
    clientStoreType = clientST; 
    clientStorePW = clientTPW; 

    trustKeyStore = trustKS; 
    trustStoreType = trustST; 
    trustStorePW = trustSPW; 
} 

private final class X509HostnameVerifierImplementation implements X509HostnameVerifier { 
    @Override 
    public void verify(String host, SSLSocket ssl) throws IOException { 
    } 

    @Override 
    public void verify(String host, X509Certificate cert) throws SSLException { 
    } 

    @Override 
    public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { 
    } 

    @Override 
    public boolean verify(String s, SSLSession sslSession) { 
     return true; 
    } 
} 

public String post(String uRL, List<NameValuePair> formparams) {   
    SSLContext sslContext = null; 
    KeyManagerFactory kmf = null; 
    TrustManagerFactory tmf = null; 
    KeyStore ks = null; 
    KeyStore tks = null; 
    try {   
     sslContext = SSLContext.getInstance(sslMode); 
     kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
     tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 

     ks = KeyStore.getInstance(clientStoreType); 
     tks = KeyStore.getInstance(trustStoreType); 

     ks.load(new FileInputStream(clientKeyStore), clientStorePW.toCharArray()); 
     tks.load(new FileInputStream(trustKeyStore), trustStorePW.toCharArray()); 

     kmf.init(ks, clientStorePW.toCharArray()); 
     tmf.init(tks); 

     sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 

    } catch (NoSuchAlgorithmException | CertificateException | IOException | KeyStoreException | UnrecoverableKeyException | KeyManagementException e1) { 
     Log4j.log.error("Error occurred: " + e1.getClass() + ":" + e1.getMessage() + ", Full Stacktrace: " + new Gson().toJson(e1.getStackTrace())); 
     return null; 
    } 

    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
      sslContext, new X509HostnameVerifierImplementation()); 

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder 
      .<ConnectionSocketFactory> create().register("https", sslsf) 
      .build(); 

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
      socketFactoryRegistry); 
    CloseableHttpClient httpclient = HttpClients.custom() 
      .setConnectionManager(cm).build(); 

    HttpPost httppost = new HttpPost(uRL); 

    UrlEncodedFormEntity uefEntity; 
    String returnCode = null; 
    try { 
     uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); 
     httppost.setEntity(uefEntity); 

     CloseableHttpResponse response = httpclient.execute(httppost); 
     try { 
      HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       returnCode = EntityUtils.toString(entity, "UTF-8"); 
      } 
     } finally { 
      response.close(); 
     } 
    } catch (ClientProtocolException e) { 
     Log4j.log.error("Error occurred: " + e.getClass() + ":" + e.getMessage() + ", Full Stacktrace: " + new Gson().toJson(e.getStackTrace())); 
     return null; 
    } catch (UnsupportedEncodingException e1) { 
     Log4j.log.error("Error occurred: " + e1.getClass() + ":" + e1.getMessage() + ", Full Stacktrace: " + new Gson().toJson(e1.getStackTrace())); 
     return null; 
    } catch (IOException e) { 
     Log4j.log.error("Error occurred: " + e.getClass() + ":" + e.getMessage() + ", Full Stacktrace: " + new Gson().toJson(e.getStackTrace())); 
     return null; 
    } finally { 
     try { 
      httpclient.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      close(httpclient); 
     } 
    } 
    return returnCode; 
} 

public void close(Closeable io) { 
    if (io != null) { 
     try { 
      io.close(); 
     } catch (IOException ignore) { 
     } 
    } 
} 

} 

Quand je l'exécute avec mes propres keystores, je suis exception tout en affichant un message

class javax.net.ssl.SSLHandshakeException:java.net.SocketException: Unrecognized Windows Sockets error: 0: recv failed 

et le serveur admin m'a donné une partie de son journal

[2017/8/21 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, WRITE: TLSv1.2 Handshake, length = 96 
[2017/8/21 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, waiting for close_notify or alert: state 1 
[2017/8/21 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, Exception while waiting for close java.net.SocketException: Unrecognized Windows Sockets error: 0: recv failed 
[2017/8/21 20:10:16:477 CST] 000000f7 SystemOut  O %% Invalidated: [Session-18, SSL_ECDHE_RSA_WITH_AES_256_CBC_SHA384] 
[2017/8/21 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, SEND TLSv1.2 ALERT: fatal, description = handshake_failure 
[2017/8/21 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, WRITE: TLSv1.2 Alert, length = 80 
[2017/8/20 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, Exception sending alert: java.net.SocketException: Unrecognized Windows Sockets error: 0: socket write error 
[2017/8/20 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, called closeSocket() 
[2017/8/20 20:10:16:477 CST] 000000f7 SystemOut  O WebContainer : 20, handling exception: javax.net.ssl.SSLHandshakeException: java.net.SocketException: Unrecognized Windows Sockets error: 0: recv failed 

serveur et j'ai ajouté le certificat de l'autre dans son propre fichier de clés de confiance, il ne devrait donc pas être question de se faire confiance. Mais je ne peux pas trouver d'autre fil qui pourrait résoudre ce problème aussi.

+0

port que vous essayez de vous connecter via SSL? – Rizwan

+0

@Rizwan 9443, l'administrateur du serveur m'a dit – user6309529

+0

Pouvez-vous confirmer si cert et cert-chain sont déjà ajoutés en tant que partie de confiance sur le serveur? Vous essayez d'accéder à un conteneur Web? – Rizwan

Répondre

0

Peut-être cela peut causer un problème:

serveur a la fois IPv4 et IPv6 activé?

Cause:

La machine virtuelle Java (JVM) peut avoir des problèmes ouverture ou la fermeture des prises au niveau du système d'exploitation lorsque les deux IPv4 et IPv6 sont activés sur un serveur Windows.

fix probable:

machine virtuelle Java devra fonctionner sur IPv4, si possible. Pour ce faire, ajoutez cette configuration l'option JVM suivante:
-Djava.net.preferIPv4Stack = true

+0

J'ai demandé à l'administrateur du serveur et il a mis cette ligne dans les arguments de démarrage et redémarré, mais ce même problème persiste – user6309529

+0

@ user6309529 mettre le même argument sur la machine client qui ouvre la connexion avec le serveur. – Rizwan

+0

Ajouté mais toujours le même jusqu'à présent, triste – user6309529