我在写一个esmtp的邮件陈程序,在很多网络里需要提供加密的socket而不是一般意义的socket,不知道哪位可以给我点范例如何使用socket,我看了api但是还是不清楚怎么用,我没有学过网络技术和相关知识,所以请麻烦说的详细点,多谢

解决方案 »

  1.   


    import java.io.*;
    import java.net.*;
    import javax.net.ssl.*;
    import java.security.cert.*;/**
     * Get a document from a web server using HTTPS. Usage:
     *   java HttpsDownload <hostname> <filename>
     **/
    public class HttpsDownload {
        public static void main(String[] args) throws IOException {
            // Get a SocketFactory object for creating SSL sockets
            SSLSocketFactory factory =
                (SSLSocketFactory) SSLSocketFactory.getDefault();        // Use the factory to create a secure socket connected to the 
            // HTTPS port of the specified web server.
            SSLSocket sslsock=(SSLSocket)factory.createSocket(args[0], // Hostname
                                                             443); // HTTPS port        // Get the certificate presented by the web server
            SSLSession session = sslsock.getSession();
            X509Certificate cert;   
            try { cert = (X509Certificate)session.getPeerCertificates()[0]; }
            catch(SSLPeerUnverifiedException e) { // If no or invalid certificate
                System.err.println(session.getPeerHost() +
                                   " did not present a valid certificate.");
                return;
            }        // Display details about the certificate
            System.out.println(session.getPeerHost() + 
                               " has presented a certificate belonging to:");
            System.out.println("\t[" + cert.getSubjectDN().getName() + "]");
            System.out.println("The certificate bears the valid signature of:");
            System.out.println("\t[" + cert.getIssuerDN().getName() + "]");        // If the user does not trust the certificate, abort
            System.out.print("Do you trust this certificate (y/n)? ");
            System.out.flush();
            BufferedReader console =
                new BufferedReader(new InputStreamReader(System.in));
            if (Character.toLowerCase(console.readLine().charAt(0)) != 'y') return;        // Now use the secure socket just as you would use a regular socket
            // First, send a regular HTTP request over the SSL socket
            PrintWriter out = new PrintWriter(sslsock.getOutputStream());
            out.print("GET " + args[1] + " HTTP/1.0\r\n\r\n");
            out.flush();        // Next, read the server's response and print it to the console
            BufferedReader in = 
             new BufferedReader(new InputStreamReader(sslsock.getInputStream()));
            String line;
            while((line = in.readLine()) != null) System.out.println(line);
            
            // Finally, close the socket
            sslsock.close(); 
        }
    }