Log4j is a nice logging solution for java developers.
http://www.vipan.com/htdocs/log4jhelp.html
http://rei1.m-plify.net/log4j/
http://www.onjava.com/pub/a/onjava/2002/08/07/log4j.html?page=2
Wednesday, September 20, 2006
Monday, September 11, 2006
downloading a web page using java code
U can use the following code to call the following methods and classes:
HttpURLConnection conn = getHttpURLConnectionUsingProxy(“http://www.google.com” "", "", "11.11.11.11", "80");
conn.connect();
InputStream xmlStream = null;
xmlStream = conn.getInputStream();
The code follows:
public HttpURLConnection getHttpURLConnectionUsingProxy(String urlstring, String username, String password, String proxyHost, String proxyPort){
String input = username + ":" + password;
String encoding = base64Encode(input);
// Enable the properties used for proxy support
System.getProperties().put("proxySet", "true" );
System.getProperties().put("proxyHost",proxyHost);
System.getProperties().put("proxyPort", proxyPort);
HttpURLConnection connection = null;
try{
URL url = new URL(urlstring);
connection = (HttpURLConnection)url.openConnection();
// Set up the connection so it knows you are sending
// proxy user information
connection.setRequestProperty( "Proxy-Authorization", encoding );
}catch(MalformedURLException murle){
System.out.println("MalformedURLException: " + murle.getMessage());
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
if (connection==null) System.out.println("Returning null connection");
return connection;
}
public String base64Encode(String s){
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
Base64OutputStream out = new Base64OutputStream(bOut);
try{
out.write(s.getBytes());
out.flush();
}catch (IOException exception){
}
return bOut.toString();
}
A separate class:
public class Base64OutputStream extends FilterOutputStream {
private int col = 0;
private int i = 0;
private int[] inbuf = new int[3];
public Base64OutputStream(OutputStream out){
super(out);
}
public void write(int c) throws IOException{
inbuf[i] = c;
i++;
if (i==3){
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]);
super.write(toBase64[inbuf[2] & 0x3F]);
col += 4;
i = 0;
if (col >= 76){
super.write('\n');
col = 0;
}
}
}
public void flush() throws IOException{
if (i==1){
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[(inbuf[0] & 0x03) << 4]);
super.write('=');
super.write('=');
}else if (i==2){
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
super.write('=');
}
}
private char[] toBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
}
Thanks to chaya Infosys, chennai
HttpURLConnection conn = getHttpURLConnectionUsingProxy(“http://www.google.com” "
conn.connect();
InputStream xmlStream = null;
xmlStream = conn.getInputStream();
The code follows:
public HttpURLConnection getHttpURLConnectionUsingProxy(String urlstring, String username, String password, String proxyHost, String proxyPort){
String input = username + ":" + password;
String encoding = base64Encode(input);
// Enable the properties used for proxy support
System.getProperties().put("proxySet", "true" );
System.getProperties().put("proxyHost",proxyHost);
System.getProperties().put("proxyPort", proxyPort);
HttpURLConnection connection = null;
try{
URL url = new URL(urlstring);
connection = (HttpURLConnection)url.openConnection();
// Set up the connection so it knows you are sending
// proxy user information
connection.setRequestProperty( "Proxy-Authorization", encoding );
}catch(MalformedURLException murle){
System.out.println("MalformedURLException: " + murle.getMessage());
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
if (connection==null) System.out.println("Returning null connection");
return connection;
}
public String base64Encode(String s){
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
Base64OutputStream out = new Base64OutputStream(bOut);
try{
out.write(s.getBytes());
out.flush();
}catch (IOException exception){
}
return bOut.toString();
}
A separate class:
public class Base64OutputStream extends FilterOutputStream {
private int col = 0;
private int i = 0;
private int[] inbuf = new int[3];
public Base64OutputStream(OutputStream out){
super(out);
}
public void write(int c) throws IOException{
inbuf[i] = c;
i++;
if (i==3){
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]);
super.write(toBase64[inbuf[2] & 0x3F]);
col += 4;
i = 0;
if (col >= 76){
super.write('\n');
col = 0;
}
}
}
public void flush() throws IOException{
if (i==1){
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[(inbuf[0] & 0x03) << 4]);
super.write('=');
super.write('=');
}else if (i==2){
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
super.write('=');
}
}
private char[] toBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
}
Thanks to chaya Infosys, chennai
Subscribe to:
Posts (Atom)