System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");Security.addProvider(newcom.sun.net.ssl.internal.ssl.Provider());// create the socket, set connect timeout to 10secsSocketsocket=newSocket();Socketsocket.connect(newInetSocketAddress(host,port),10*1000);//Create a trust manager that does not validate certificate chains:TrustManager[]tm=newTrustManager[]{newX509TrustManager(){publicX509Certificate[]getAcceptedIssuers(){returnnull;}publicvoidcheckServerTrusted(X509Certificate[]certs,StringauthType)throwsCertificateException{return;}publicvoidcheckClientTrusted(X509Certificate[]certs,StringauthType)throwsCertificateException{return;}}};SSLContextsc;try{sc=SSLContext.getInstance("SSL");sc.init(null,tm,newSecureRandom());}catch(Exceptione){thrownewIOException("Failed while creating SSLSocket: "+e.getMessage());}// wrap out socket to secure socketSSLSocketFactorysocketfactory=sc.getSocketFactory();socket=(SSLSocket)socketfactory.createSocket(socket,host,port,true);// now use it like a common socketOutputStreamout=socket.getOutputStream();InputStreamin=socket.getInputStream();
0 Comments