package ru.turumbay.forum34
// пример коннекта к домине с авторизацией по http
import java.io.*;
import java.net.*;
class DominoHTTPConnection{
private String serverAddress = "";
private String sessionId = "";
DominoHTTPConnection( String serverAddress , String sessionId ){
this.serverAddress = serverAddress;
this.sessionId = sessionId;
}
DominoHTTPConnection( String serverAddress , String login , String password ){
this.serverAddress = serverAddress;
login( login , password );
}
private void login(String login,String password) {
HttpURLConnection 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("") ) /* (sessionId == "" ) */ // см. замечание nvy двумя постами ниже
throw new IllegalArgumentException("Can't create session");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeConnection( connection );
}
}
private void closeConnection( HttpURLConnection connection ){
if (connection != null) {
connection.disconnect();
}
}
private HttpURLConnection doPost(URL url, String postData, String sessionId) throws IOException {
HttpURLConnection connection = (HttpURLConnection) 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 ){
HttpURLConnection 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('\r');
}
return response.toString();
} finally {
rd.close();
}
}
public String getSessionId(){
return sessionId;
}
}
public class NvyExample {
public static void main( String [] args ){
DominoHTTPConnection domino = new DominoHTTPConnection( "http://domino.acme.com" , "login" , "password" );
String res = domino.doPost( "/replicaId/_/unid" , "" );
System.out.println( res );
// новый запрос с использованием старой куки
DominoHTTPConnection anotherDominoConnection = new DominoHTTPConnection( "http://domino.acme.com" , domino.getSessionId() );
}
}