Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2012 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 package org.apache.commons.httpclient.contrib.ssl;

EasySSLProtocolSocketFactory can be used to creats SSL that accept self-signed certificates.

This socket factory SHOULD NOT be used for productive systems due to security reasons, unless it is a concious decision and you are perfectly aware of security implications of accepting self-signed certificates

Example of using custom protocol socket factory for a specific host:

     Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);

     URI uri = new URI("https://localhost/", true);
     // use relative url only
     GetMethod httpget = new GetMethod(uri.getPathQuery());
     HostConfiguration hc = new HostConfiguration();
     hc.setHost(uri.getHost(), uri.getPort(), easyhttps);
     HttpClient client = new HttpClient();
     client.executeMethod(hc, httpget);

Example of using custom protocol socket factory per default instead of the standard one:

     Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
     Protocol.registerProtocol("https", easyhttps);

     HttpClient client = new HttpClient();
     GetMethod httpget = new GetMethod("https://localhost/");

Oleg Kalnichevski

DISCLAIMER: HttpClient developers DO NOT actively support this component. The component is provided as a reference material, which may be inappropriate for use without additional customization.

 public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory {

Log object for this class.
     private static final Logger LOG = Logger.getLogger(EasySSLProtocolSocketFactory.class);
     private SSLContext sslcontext = null;

Constructor for EasySSLProtocolSocketFactory.
     public EasySSLProtocolSocketFactory() {
     private static SSLContext createEasySSLContext() {
         try {
             SSLContext context = SSLContext.getInstance("SSL");
              new TrustManager[] {new EasyX509TrustManager(null)}, 
            return context;
        } catch (Exception e) {
            .error(e.getMessage(), e);
            throw new HttpClientError(e.toString());
    private SSLContext getSSLContext() {
        if (this. == null) {
            this. = createEasySSLContext();
        return this.;

    public Socket createSocket(
        String host,
        int port,
        InetAddress clientHost,
        int clientPort)
        throws IOExceptionUnknownHostException {
        return getSSLContext().getSocketFactory().createSocket(

Attempts to get a new socket connection to the given host within the given time limit.

To circumvent the limitations of older JREs that do not support connect timeout a controller thread is executed. The controller thread attempts to create a new socket within the given limit of time. If socket constructor does not return until the timeout expires, the controller terminates and throws an org.apache.commons.httpclient.ConnectTimeoutException

host the host name/IP
port the port on the host
localAddress the local host name/IP to bind the socket to
localPort the port on the local machine
params Http connection parameters
Socket a new socket
Throws: if an I/O error occurs while creating the socket if the IP address of the host cannot be determined
    public Socket createSocket(
        final String host,
        final int port,
        final InetAddress localAddress,
        final int localPort,
        final HttpConnectionParams params
        if (params == null) {
            throw new IllegalArgumentException("Parameters may not be null");
        int timeout = params.getConnectionTimeout();
        SocketFactory socketfactory = getSSLContext().getSocketFactory();
        if (timeout == 0) {
            return socketfactory.createSocket(hostportlocalAddresslocalPort);
        } else {
            Socket socket = socketfactory.createSocket();
            SocketAddress localaddr = new InetSocketAddress(localAddresslocalPort);
            SocketAddress remoteaddr = new InetSocketAddress(hostport);
            return socket;

    public Socket createSocket(String hostint port)
        throws IOExceptionUnknownHostException {
        return getSSLContext().getSocketFactory().createSocket(

    public Socket createSocket(
        Socket socket,
        String host,
        int port,
        boolean autoClose)
        throws IOExceptionUnknownHostException {
        return getSSLContext().getSocketFactory().createSocket(
    public boolean equals(Object obj) {
        return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class));
    public int hashCode() {
        return EasySSLProtocolSocketFactory.class.hashCode();
New to GrepCode? Check out our FAQ X