gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:15 24 4
gpt4 key购买 nike

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

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);
......
}

关于没有HTTP或HTTPS通信的android ssl通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31303547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com