Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 6.52 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. What change in xml-rpc in android application to use HTTPS communication
  2. //In class XMLRPCClient
  3.     public XMLRPCClient(URI uri) {
  4.                     SchemeRegistry registry = new SchemeRegistry();
  5.                     registry.register(new Scheme("http", new PlainSocketFactory(),
  6.                             80));
  7.                     registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
  8.  
  9.                     postMethod = new HttpPost(uri);
  10.                     postMethod.addHeader("Content-Type", "text/xml");
  11.  
  12.                     // WARNING
  13.                     // I had to disable "Expect: 100-Continue" header since I had
  14.                     // two second delay between sending http POST request and POST body
  15.                     httpParams = postMethod.getParams();
  16.                     HttpProtocolParams.setUseExpectContinue(httpParams, false);
  17.                     this .client = new DefaultHttpClient(
  18.                             new ThreadSafeClientConnManager(httpParams, registry),
  19.                             httpParams);
  20.                 }
  21.        
  22. package org.xmlrpc.android;
  23.  
  24. /*
  25.  * Licensed to the Apache Software Foundation (ASF) under one
  26.  * or more contributor license agreements.  See the NOTICE file
  27.  * distributed with this work for additional information
  28.  * regarding copyright ownership.  The ASF licenses this file
  29.  * to you under the Apache License, Version 2.0 (the
  30.  * "License"); you may not use this file except in compliance
  31.  * with the License.  You may obtain a copy of the License at
  32.  *
  33.  *   http://www.apache.org/licenses/LICENSE-2.0
  34.  *
  35.  * Unless required by applicable law or agreed to in writing,
  36.  * software distributed under the License is distributed on an
  37.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38.  * KIND, either express or implied.  See the License for the
  39.  * specific language governing permissions and limitations
  40.  * under the License.
  41.  */
  42.  
  43. import java.io.IOException;
  44. import java.net.InetAddress;
  45. import java.net.InetSocketAddress;
  46. import java.net.Socket;
  47. import java.net.UnknownHostException;
  48.  
  49. import javax.net.ssl.SSLContext;
  50. import javax.net.ssl.SSLSocket;
  51. import javax.net.ssl.TrustManager;
  52.  
  53. import org.apache.http.conn.ConnectTimeoutException;
  54. import org.apache.http.conn.scheme.LayeredSocketFactory;
  55. import org.apache.http.conn.scheme.SocketFactory;
  56. import org.apache.http.params.HttpConnectionParams;
  57. import org.apache.http.params.HttpParams;
  58.  
  59. /**
  60.  * This socket factory will create ssl socket that accepts self signed certificate
  61.  *
  62.  * @author olamy
  63.  * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse $
  64.  * @since 1.2.3
  65.  */
  66. public class EasySSLSocketFactory implements SocketFactory, LayeredSocketFactory {
  67.  
  68.     private SSLContext sslcontext = null;
  69.  
  70.     private static SSLContext createEasySSLContext() throws IOException {
  71.         try {
  72.             SSLContext context = SSLContext.getInstance("TLS");
  73.             context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
  74.             return context;
  75.         } catch (Exception e) {
  76.             throw new IOException(e.getMessage());
  77.         }
  78.     }
  79.  
  80.     private SSLContext getSSLContext() throws IOException {
  81.         if (this.sslcontext == null) {
  82.             this.sslcontext = createEasySSLContext();
  83.         }
  84.         return this.sslcontext;
  85.     }
  86.  
  87.     /**
  88.      * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, java.lang.String, int,
  89.      *      java.net.InetAddress, int, org.apache.http.params.HttpParams)
  90.      */
  91.     public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
  92.             HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
  93.         int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
  94.         int soTimeout = HttpConnectionParams.getSoTimeout(params);
  95.         InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
  96.         SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());
  97.  
  98.         if ((localAddress != null) || (localPort > 0)) {
  99.             // we need to bind explicitly
  100.             if (localPort < 0) {
  101.                 localPort = 0; // indicates "any"
  102.             }
  103.             InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
  104.             sslsock.bind(isa);
  105.         }
  106.  
  107.         sslsock.connect(remoteAddress, connTimeout);
  108.         sslsock.setSoTimeout(soTimeout);
  109.         return sslsock;
  110.  
  111.     }
  112.  
  113.     /**
  114.      * @see org.apache.http.conn.scheme.SocketFactory#createSocket()
  115.      */
  116.     public Socket createSocket() throws IOException {
  117.         return getSSLContext().getSocketFactory().createSocket();
  118.     }
  119.  
  120.     /**
  121.      * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket)
  122.      */
  123.     public boolean isSecure(Socket socket) throws IllegalArgumentException {
  124.         return true;
  125.     }
  126.  
  127.     /**
  128.      * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, java.lang.String, int,
  129.      *      boolean)
  130.      */
  131.     public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
  132.             UnknownHostException {
  133.         return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
  134.     }
  135.  
  136.     // -------------------------------------------------------------------
  137.     // javadoc in org.apache.http.conn.scheme.SocketFactory says :
  138.     // Both Object.equals() and Object.hashCode() must be overridden
  139.     // for the correct operation of some connection managers
  140.     // -------------------------------------------------------------------
  141.  
  142.     public boolean equals(Object obj) {
  143.         return ((obj != null) && obj.getClass().equals(EasySSLSocketFactory.class));
  144.     }
  145.  
  146.     public int hashCode() {
  147.         return EasySSLSocketFactory.class.hashCode();
  148.     }
  149.  
  150. }
  151.        
  152. package org.xmlrpc.android;
  153.  
  154. import java.security.KeyStore;
  155. import java.security.KeyStoreException;
  156. import java.security.NoSuchAlgorithmException;
  157. import java.security.cert.CertificateException;
  158. import java.security.cert.X509Certificate;
  159.  
  160. import javax.net.ssl.X509TrustManager;
  161.  
  162. class EasyX509TrustManager implements X509TrustManager
  163. {
  164.     public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
  165.         super();
  166.  
  167.     }
  168. @Override
  169. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException
  170. {
  171.  
  172. }
  173.  
  174. @Override
  175. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
  176. {
  177.  
  178. }
  179.  
  180. @Override
  181. public X509Certificate[] getAcceptedIssuers()
  182. {
  183. return new X509Certificate[0];
  184. }
  185. }