import java.net.*;
import java.io.*;


public class LogHTTP { 
	protected String cred;
	protected String baseURL;

	public LogHTTP(String b, String username, String password) {
		baseURL=b;
			
		if ( null == username || null == password ) {
			cred=null;
		} else {
			cred = username + ":" + password; 
		}
	}

	public LogHTTP(IniFile ini) {
		String u,p;

		baseURL=ini.getValue("LOGHTTP","baseURL");
		u=ini.getValue("LOGHTTP","username");
		p=ini.getValue("LOGHTTP","password");

		if ( null == u || null == p ) {
			cred=null;
		} else {
			cred = u + ":" + p; 
		}
	}

	public void sendRecord(String record) {
		try {
			String url = baseURL + URLEncoder.encode(record,"utf-8");

			System.err.println("# requesting: " + url);

			/* connect to the server */
			URL u = new URL(url);
			URLConnection uc = u.openConnection();

			if ( null != cred ) {
				/* we need basic auth */
				String encoding = new sun.misc.BASE64Encoder().encode (cred.getBytes());
				uc.setRequestProperty ("Authorization", "Basic " + encoding);
			}

			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setDoInput(true); 
			connection.setRequestMethod("GET");
      
			/* Read the response */
			InputStream in = connection.getInputStream(); 
			String line; 

			BufferedReader is = new BufferedReader(new InputStreamReader(in));

			System.out.println("################## LogHTTP server responded ######################");
			while ( (line = is.readLine()) != null) {
				System.out.println(line);
			}
			System.out.println("########################### done #################################");
		
			in.close();
			connection.disconnect();
		} catch (IOException e) {
			System.err.println(e); 
		}
	}
}
