package YOUR_PACKAGE;
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
public class HTTPSConnectionExample {
public static void main(String[] args ){
try {
String urlString = "https://YUOR_SERVER";
String login = "YOUR_LOGIN";
String password = "YOUR_PASSWORD";
DominoHTTPSConnection domino = new DominoHTTPSConnection(urlString, login, password);
String content = "YOUR_XML_REQUEST";
System.out.println(content);
urlString = "YOUR_DATABASE.nsf/YOUR_WEBSERVICE/";
String res = domino.doPost(urlString, content);
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
}
}
static class DominoHTTPSConnection{
private String serverAddress = "";
private String sessionId = "";
DominoHTTPSConnection(String serverAddress, String sessionId){
this.serverAddress = serverAddress;
this.sessionId = sessionId;
}
DominoHTTPSConnection(String serverAddress, String login, String password){
this.serverAddress = serverAddress;
login(login, password);
}
private void login(String login, String password) {
HttpsURLConnection connection = null;
try {
String loginData = "&Username=" + login + "&Password=" + password;
URL url = new URL(serverAddress + "/names.nsf?login");
connection = doPost(url, loginData, "");
sessionId = getSessionId(connection);
if (sessionId.equals("")) throw new IllegalArgumentException("Can't create session");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeConnection(connection);
}
}
private void closeConnection(HttpsURLConnection connection){
if (connection != null) {
connection.disconnect();
}
}
private HttpsURLConnection doPost(URL url, String postData, String sessionId) throws IOException {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
//End create a trust manager that does not validate certificate chains
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString(postData.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setRequestProperty("Cookie", sessionId);
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(postData);
wr.flush();
wr.close();
return connection;
}
private String getSessionId(HttpURLConnection connection) {
String cookieVal = connection.getHeaderField("Set-Cookie");
if (cookieVal == null)
return "";
return cookieVal.substring(0, cookieVal.indexOf(";"));
}
public String doPost( String path , String postData ){
HttpsURLConnection connection = null;
try {
URL url = new URL( serverAddress + path );
connection = doPost( url , postData , sessionId);
return getResponseText( connection );
} catch ( Exception e ){
throw new RuntimeException( e );
} finally {
closeConnection( connection );
}
}
private String getResponseText( HttpURLConnection connection ) throws IOException{
BufferedReader rd = null;
try {
rd = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append("\n\r");
}
return response.toString();
} finally {
rd.close();
}
}
public String getSessionId(){
return sessionId;
}
}
}