2011-11-11 57 views
4

我在android上編寫應用程序,它將通過SSL與服務器連接並與他交換一些數據。這裏是我的服務器代碼:在Android上使用SSL

import javax.net.ssl.SSLServerSocket; 
import javax.net.ssl.SSLServerSocketFactory; 
import javax.net.ssl.SSLSocket; 
import java.io.BufferedReader; 
import java.io.InputStream; 

import java.io.InputStreamReader; 


public class EchoServer { 
public static void main(String[] arstring) { 
    try { 
     SSLServerSocketFactory sslserversocketfactory = 
       (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); 
     SSLServerSocket sslserversocket = 
       (SSLServerSocket) sslserversocketfactory.createServerSocket(23423); 
     SSLSocket sslsocket = (SSLSocket) sslserversocket.accept(); 

     InputStream inputstream = sslsocket.getInputStream(); 
     InputStreamReader inputstreamreader = new InputStreamReader(inputstream); 
     BufferedReader bufferedreader = new BufferedReader(inputstreamreader); 

     String string = null; 
     while ((string = bufferedreader.readLine()) != null) { 
      System.out.println("odpowiedz z servera: "+string); 
      System.out.flush(); 
     } 
    } catch (Exception exception) { 
     exception.printStackTrace(); 
    } 
} 
} 

,這裏是我的客戶:

package andr.andr; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 

import javax.net.ssl.SSLSocket; 
import javax.net.ssl.SSLSocketFactory; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 

public class Androidssl2Activity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

public void fun(View view) 
{ 
    try { 
     SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 
     SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("87.206.146.154", 23423); 

     InputStream inputstream = System.in; 
     InputStreamReader inputstreamreader = new InputStreamReader(inputstream); 
     BufferedReader bufferedreader = new BufferedReader(inputstreamreader); 

     OutputStream outputstream = sslsocket.getOutputStream(); 
     OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream); 
     BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter); 

     String string = "Echo test"; 

      bufferedwriter.write(string + '\n'); 
      bufferedwriter.flush(); 

    } catch (Exception exception) { 
     exception.printStackTrace(); 
    } 
} 
} 

我的,我不知道如何把證書android應用問題。我不能使用啓動參數:

的Java -Djavax.net.ssl.keyStore = mySrvKeystore -Djavax.net.ssl.keyStorePassword = 123456 EchoServer的

的Java -Djavax.net.ssl.trustStore = mySrvKeystore -Djavax.net.ssl.trustStorePassword = 123456 EchoClient

就像普通的java項目一樣。所以我怎麼才能將證書指向android應用程序。我谷歌很多,沒有找到任何東西,但我不相信我是唯一有問題的人。任何人都可以解釋清楚我該怎麼做。謝謝你的幫助。

回答

1

↳android.net.SSLCertificateSocketFactory爲您的工廠和System.setprop爲您的-D選項也許?