繁体   English   中英

没有HTTP或HTTPS通信的android ssl通信

[英]android ssl communication without HTTP or HTTPS communication

嗨,我是Android平台上的新手。 我需要在我的应用程序中编写一个TCP / SSL客户端类,该类使用某些c#服务器下载文本文件。 我已经创建了.cer文件和bks文件用于ssl通信。 实际上,我需要使用套接字类通信而不是HTTP或HTTPS通信。 我的Android代码是:

public class MainActivity extends Activity {
static String filename = null;
static Socket socket = null;
static Boolean flag = true;
Button GetServerData, Upload;
TextView textPort;
EditText et;
Context context;static final int SocketServerPORT = 8889;
static final String SocketServerIP = "169.254.80.80";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button GetServerData = (Button) findViewById(R.id.GetServerData);

    GetServerData.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Intent i = new Intent(MainActivity.this, MyHttpClient.class);
            // startActivity(i);
            ClientRxThread clientRxThread = new ClientRxThread(
                    SocketServerIP, SocketServerPORT);

            clientRxThread.start();}
    });

}private class ClientRxThread extends Thread {
    String dstAddress;
    int dstPort;

    ClientRxThread(String address, int port) {
        dstAddress = address;
        dstPort = port;
    }

    @Override
    public void run() {
        // Socket socket = null;
        InputStream in;
        int bufferSize = 0;

        try {

            socket = new Socket(dstAddress, dstPort);

            File file = new File(Environment.getExternalStorageDirectory(),
                    "test.txt");
            bufferSize = socket.getReceiveBufferSize();
            in = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream output = new BufferedOutputStream(fos);
            DataInputStream clientData = new DataInputStream(in);
            byte[] buffer = new byte[bufferSize];
            int read;
            while ((read = clientData.read(buffer)) > 0) {// != -1) {
                output.write(buffer, 0, read);
            }
            output.flush();
            output.close();
            socket.close();

            MainActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, "Finished",
                            Toast.LENGTH_LONG).show();
                }});
        } catch (IOException e) {

            e.printStackTrace();

            final String eMsg = "Something wrong: " + e.getMessage();
            MainActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, eMsg,
                            Toast.LENGTH_LONG).show();
                }
            });

        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}}

您是否尝试过SSLSocket,其用法就像普通的Socket一样。

private void initSslSocketFactory() {

    try {
        /*
         * SETUP TRUSTSTORE 
         */  
        KeyStore trustStore = KeyStore.getInstance("BKS");  
        TrustManagerFactory trustManagerFactory = TrustManagerFactory  
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());  
        InputStream trustStoreStream = context.getResources()  
            .openRawResource(R.raw.ca);  
        trustStore.load(trustStoreStream, "000000".toCharArray());  
        trustManagerFactory.init(trustStore);  

        /* 
         * SETUP KEYSTORE 
         */  
        KeyStore keyStore = KeyStore.getInstance("PKCS12");  
        KeyManagerFactory keyManagerFactory = KeyManagerFactory  
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());  
        InputStream keyStoreStream = context.getResources()  
            .openRawResource(R.raw.client);  
        keyStore.load(keyStoreStream, "000000".toCharArray());  
        keyManagerFactory.init(keyStore, "000000".toCharArray());  

        /* 
         * SETUP the SSL context to use the truststore and keystore 
         */   
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
        mSslSocketFactory = sslContext.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        Log.e(Tag, "" + e.getMessage());
        e.printStackTrace();
    } catch (KeyManagementException e) {
        Log.e(Tag, "" + e.getMessage());
        e.printStackTrace();
    }
}

public void connect() {
    mSslSock = (SSLSocket) mSslSocketFactory.createSocket();
    mSslSock.connect(new InetSocketAddress(host, port));
    mSslSock.setSoTimeout(SOCKET_TIMEOUT);
    mSslSock.setUseClientMode(true);
    mSslSock.setEnabledCipherSuites(mSslSock.getEnabledCipherSuites());
    mSslSock.setEnabledProtocols(mSslSock.getSupportedProtocols());
    mSslSock.connect(SocketAddress);
    ......
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM