Веб-клиент на основе BCJAVA

Программисты JAVA общаются здесь
Anonymous
 Веб-клиент на основе BC

Сообщение Anonymous »

Я ищу исходный код веб -клиента, который использует классы Bouncy Castle, для доступа к веб -серверу. До сих пор я нашел слишком старый исходный код, основанный на классах, которые устарели или около того. Причина, по которой я ищу код на основе BC, диктуется требованиями проекта. />public static void connectJettyBcNoAuth() {
try {
java.security.SecureRandom secureRandom = new java.security.SecureRandom();
Socket socket = new Socket(java.net.InetAddress.getByName("10.164.1.102"), 443);
TlsClientProtocol protocol = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream());
TlsCrypto crypto = new BcTlsCrypto();
DefaultTlsClient client = new DefaultTlsClient(crypto) {
public TlsAuthentication getAuthentication() throws IOException {
TlsAuthentication auth = new TlsAuthentication() {
@Override
public void notifyServerCertificate(TlsServerCertificate arg0) throws IOException {
// TODO Auto-generated method stub

}

public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException {
return null;
}

};
return auth;
}
};
protocol.connect(client);
OutputStream output = protocol.getOutputStream();
output.write("GET /index.html HTTP/1.1\r\nHost: 10.164.1.102\r\n".getBytes("UTF-8"));
output.write("Connection: close\r\n".getBytes("UTF-8")); // So the server will close socket immediately.
output.write("\r\n".getBytes("UTF-8")); // HTTP1.1 requirement: last line must be empty line.
output.flush();

java.io.InputStream input = protocol.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}

}
< /code>
Что мне действительно нужно, - это способ получить доступ к серверу, когда сервер запрашивается аутентификация клиента. Я пытался с чем -то похожим на это: < /p>
public static void connectJettyBcWithAuth() {
try {
// Load the Windows-MY keystore and get client private key and cert
// ...

// Load the truststore for server cert
// ...

java.security.SecureRandom secureRandom = new java.security.SecureRandom();
Socket socket = new Socket(java.net.InetAddress.getByName("10.164.1.102"), 443);
TlsClientProtocol protocol = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream());
TlsCrypto crypto = new BcTlsCrypto();
DefaultTlsClient client = new DefaultTlsClient(crypto) {
public TlsAuthentication getAuthentication() throws IOException {
return new TlsAuthentication() {
@Override
public void notifyServerCertificate(TlsServerCertificate serverCertificate) throws IOException {
// Handle server certificate
}

@Override
public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException {
// tried many different approaches, none was successful or cannot be compiled
}
};
}
};
protocol.connect(client);

OutputStream output = protocol.getOutputStream();
output.write("GET /index.html HTTP/1.1\r\nHost: 10.164.1.102\r\n".getBytes("UTF-8"));
output.write("Connection: close\r\n".getBytes("UTF-8")); // So the server will close socket immediately.
output.write("\r\n".getBytes("UTF-8")); // HTTP1.1 requirement: last line must be empty line.
output.flush();

java.io.InputStream input = protocol.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
< /code>
Просмотр исходного кода теста BC, я не смог найти ни одного примера. Любая помощь будет оценена.

Подробнее здесь: https://stackoverflow.com/questions/794 ... web-client

Вернуться в «JAVA»