jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question How do I create an SSL socket connection from a Java application?
Derived from A question posed by John Mitchell PREMIUM
Topics Java:API:Security, Java:API:Networking
Author Benoit Xhenseval
Created Apr 5, 2000 Modified May 2, 2000


Answer
If you have the JSSE package available, you will be doing this in order to use the default implementation from Sun:

import javax.net.ssl.*;
import java.io.*;
import java.net.*;
import java.security.*;

public class SSLSocketClient {

  public static void main(String args[]) throws Exception {

    String urlString = (args.length == 1) ? 
      args[0] : "http://www.verisign.com/index.html";
    URL url = new URL(urlString);

    Security.addProvider(
      new com.sun.net.ssl.internal.ssl.Provider());

    SSLSocketFactory factory = 
      (SSLSocketFactory)SSLSocketFactory.getDefault();
    SSLSocket socket = 
      (SSLSocket)factory.createSocket(url.getHost(), 443);

    PrintWriter out = new PrintWriter(
        new OutputStreamWriter(
          socket.getOutputStream()));
    out.println("GET " + urlString + " HTTP/1.1");
    out.println();
    out.flush();

    BufferedReader in = new BufferedReader(
      new InputStreamReader(
      socket.getInputStream()));

    String line;

    while ((line = in.readLine()) != null) {
      System.out.println(line);
    }

    out.close();
    in.close();
  }
}


Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 0



Comments and alternative answers

Comment on this FAQ entry

If you want to access a secure (HTTPS) website and...
Patrick Balm, Jun 8, 2000  [replies:1]
If you want to access a secure (HTTPS) website and you are confronted with a UID/PWD challenge then this is the solution:

// Simply done with existing classes :-)
import java.net.*;
import java.io.*;

public class XS2SSL {
    public static void main(String[] args) throws Exception {

	// Dynamic registration of JSSE provider
	java.security.Security.addProvider(
          new com.sun.net.ssl.internal.ssl.Provider());

    // Need to be set
    	  System.setProperty(
            "java.protocol.handler.pkgs",
            "com.sun.net.ssl.internal.www.protocol");

    // Install Authenticator
    Authenticator.setDefault (new PasswordAuthenticator());

	URL url = new URL("https://your-UID+PWD-secured-site-URL-here");
	BufferedReader in = new BufferedReader(
			new InputStreamReader(
			url.openStream()));

	String inputLine;

	while ((inputLine = in.readLine()) != null)
	    System.out.println(inputLine);

	in.close();
    }
}

// --- PasswordAuthenticator class

import java.net.Authenticator;
import java.net.PasswordAuthentication;

class PasswordAuthenticator extends Authenticator {

    public ExperianCreditAuthenticator() {
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        System.out.println("getPasswordAuthentication() called for https connection!!!");
        return new PasswordAuthentication(strUser, "password".toCharArray());
    }
}
You must use JDK 1.2.1 for this to work (Authenticator class)
and JSSE (Java Secure Sockets Extension).

This was tested with JSSE-1.0.1

Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment  Help  

Re: If you want to access a secure (HTTPS) website and...
shiva prasad, May 21, 2004
Is Authenticator used for this scenario? A user accessing browser through a corporate firewall. A popup window comes up for userid/passwd..

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  


Ask A Question



 
Related Links

Security FAQ

Security Forum

Java Security Home

Java Security Documentation

jGuru.com Fundamentals of Java Security tutorial

The Java Secure Socket Extension (JSSE) with SSL support

Networking FAQ

Networking Forum

Java Tutorial Networking Trail

Sun's Networking Features Documentation

Wish List
Features
About jGuru
Contact Us

 


Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers