我正在尝试使用Android中的SSLSocket将数据发送到服务器并从服务器接收响应。建立安全连接后,我可以将数据发送到服务器,但是无法收到服务器的响应。
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SocketFactory socketFactory = (SocketFactory) sc.getSocketFactory();
socket = (SSLSocket) socketFactory.createSocket(SERVER_IP,SERVERPORT);
printSocketInfo(socket);
socket.startHandshake();
printServerCertificate(socket);
if (socket.isConnected())
Log.d(TAG,"connected");
dataOutputStream = new DataOutputStream(
socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
InputStream inFromServer = socket.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
// transfer JSONObject as String to the server
dataOutputStream.writeUTF(jsonData.toString());
Log.i(TAG, "waiting for response from host");
// Thread will wait till server replies
String response = dataInputStream.readUTF();
if (response != null) {
success = true;
Log.i(TAG, response);
} else {
success = false;
}
服务器接收从客户端发送的数据,但是客户端无法从服务器接收或读取响应。
有人可以告诉我如何使用SSLSocket
从服务器接收响应。