嗨,我是Android平台上的新手。我需要在我的应用程序中编写一个TCP / SSL客户端类,它使用一些c#server下载文本文件。我已经为ssl通信创建了.cer文件和bks文件。实际上我需要使用套接字类通信而不是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();
}
}
}
}
}}
答案 0 :(得分:0)
你有没有尝试过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);
......
}