• HTTPS запрос в Android?

    @themir Автор вопроса
    Ответ на мой вопрос:

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);
                    
    SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                    
    httpParams = new BasicHttpParams();
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(httpParams, true);
                    
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    sr.register(new Scheme("https", sf, 443));
    ccManager = new ThreadSafeClientConnManager(httpParams, sr);
    
    HttpClient client = = new DefaultHttpClient(ccManager, httpParams);  	
    HttpPost post = new HttpPost(url);
    post.setEntity(new UrlEncodedFormEntity(params));
    HttpResponse responce = client.execute(post);       		  
    in = new BufferedReader(new InputStreamReader(responce.getEntity().getContent()));    		
    String line = "";           	
    while ((line = in.readLine()) != null) sb.append(line);     	
    in.close();


    класс MySSLSocketFactory:
    public class MySSLSocketFactory extends SSLSocketFactory {
    	private SSLContext sslContext = SSLContext.getInstance("TLS");
    
    	public MySSLSocketFactory(KeyStore truststore)
    			throws NoSuchAlgorithmException, KeyManagementException,
    			KeyStoreException, UnrecoverableKeyException {
    		super(truststore);
    
    		TrustManager tm = new X509TrustManager() {
    			public void checkClientTrusted(X509Certificate[] chain,
    					String authType) throws CertificateException {
    			}
    
    			public void checkServerTrusted(X509Certificate[] chain,
    					String authType) throws CertificateException {
    			}
    
    			public X509Certificate[] getAcceptedIssuers() {
    				return null;
    			}
    		};
    
    		sslContext.init(null, new TrustManager[] { tm }, null);
    	}
    
    	@Override
    	public Socket createSocket(Socket socket, String host, int port,
    			boolean autoClose) throws IOException, UnknownHostException {
    		return sslContext.getSocketFactory().createSocket(socket, host, port,
    				autoClose);
    	}
    
    	@Override
    	public Socket createSocket() throws IOException {
    		return sslContext.getSocketFactory().createSocket();
    	}
    }
    Ответ написан
    Комментировать