From 5da979b312ed191370b3545cc3a958d32a1f497d Mon Sep 17 00:00:00 2001 From: tilman Date: Thu, 19 Jul 2012 10:33:57 +0200 Subject: [PATCH] Logger --- WebCallerID/pom.xml | 42 +++-- .../de/tilman/callerId/CallerIdClient.java | 156 ++++-------------- 2 files changed, 48 insertions(+), 150 deletions(-) diff --git a/WebCallerID/pom.xml b/WebCallerID/pom.xml index afc7d2d..b563558 100755 --- a/WebCallerID/pom.xml +++ b/WebCallerID/pom.xml @@ -1,23 +1,21 @@ - - 4.0.0 - WebCallerID - WebCallerID - 0.0.1-SNAPSHOT - - - ant - ant-apache-log4j - 1.6.5 - - - org.apache.commons - commons-lang3 - 3.1 - - - org.apache.httpcomponents - httpclient - 4.2.1 - - + + 4.0.0 + WebCallerID + WebCallerID + 0.0.1-SNAPSHOT + + + log4j + log4j + 1.2.17 + + + org.apache.commons + commons-lang3 + 3.0.1 + jar + compile + + \ No newline at end of file diff --git a/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java b/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java index 7529da7..76c4ecf 100755 --- a/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java +++ b/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java @@ -9,36 +9,23 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.Scanner; -import org.apache.http.HttpEntity; -import org.apache.http.HttpRequest; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.ProtocolException; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.client.DefaultRedirectStrategy; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.protocol.HttpContext; -import org.apache.http.util.EntityUtils; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; public class CallerIdClient { - - DefaultHttpClient httpclient = new DefaultHttpClient(); + + private final static Logger log = Logger.getLogger(CallerIdClient.class); String password = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode("53795306".getBytes()); String cookie; public CallerIdClient() { - - // com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(null); - - // plain Java + BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} - %m%n"))); + try { login(); getList(); @@ -46,20 +33,12 @@ public class CallerIdClient { } catch (Exception e1) { e1.printStackTrace(); } - - // Apache http client - // try { - // apacheLogin(); - // } catch (Exception e) { - // e.printStackTrace(); - // } - } private void login() throws Exception { - System.out.println("--- login() --------------------------------------"); + log.info("login()"); String body = "Username=" + URLEncoder.encode("admin", "UTF-8") + "&" + "Password=" + password; - System.out.println(body); + log.info(body); URL url2 = new URL("https://speedport.ip/index/login.cgi"); HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); @@ -92,9 +71,7 @@ public class CallerIdClient { } private void getList() throws Exception { - System.out.println("--- getList() --------------------------------------"); - //https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3,xctyNRvrwuh - + log.info("getList()"); URL u = new URL("https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); URLConnection urlConn = u.openConnection(); String cookieString = "SessionID_R3=" + cookie; @@ -106,104 +83,27 @@ public class CallerIdClient { } private void logout() throws Exception { - System.out.println("--- logout() --------------------------------------"); - URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie); - URLConnection urlConn = myUrl.openConnection(); + log.info("logout()"); +// URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie); + URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?cookie=SessionID_R3," + cookie); String cookieString = "SessionID_R3=" + cookie; + URLConnection urlConn = myUrl.openConnection(); urlConn.setRequestProperty("Cookie", cookieString); + urlConn.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); urlConn.connect(); - - // URL u = new URL("https://speedport.ip/pub/top_beenden.php"); - // // https://speedport.ip/logout.cgi?RequestFile=/pub/top_beenden.php - // String r = new Scanner(u.openStream()).useDelimiter("\\Z").next(); - // System.out.println("Logout:"); - // System.out.println(r); + showStatus(urlConn); + + myUrl = new URL("https://speedport.ip/"); + urlConn = myUrl.openConnection(); + urlConn.setRequestProperty("Cookie", cookieString); + urlConn.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); + urlConn.connect(); + showStatus(urlConn); } - - private void plainJava() throws Exception { - - URL url = new URL("https://speedport.ip/"); - URLConnection conn = url.openConnection(); - - for (int i = 0;; i++) { - String name = conn.getHeaderFieldKey(i); - String value = conn.getHeaderField(i); - if (name == null && value == null) { - break; - } - if (name == null) { - System.out.println("Server HTTP version, Response code:"); - System.out.println(value); - System.out.print("\n"); - } else { - System.out.println(name + "=" + value); - } - } - - // Startseite - URL u = new URL("https://speedport.ip/"); - String r = new Scanner(u.openStream()).useDelimiter("\\Z").next(); - System.out.println(r); - - } - - - private void apacheLogin() throws Exception { - httpclient.setRedirectStrategy(new DefaultRedirectStrategy() { - public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) { - boolean isRedirect = false; - try { - isRedirect = super.isRedirected(request, response, context); - } catch (ProtocolException e) { - e.printStackTrace(); - } - if (!isRedirect) { - int responseCode = response.getStatusLine().getStatusCode(); - if (responseCode == 301 || responseCode == 302) { - return true; - } - } - return isRedirect; - } - }); - - /* - * // Apache 1 HttpPost httpPost = new - * HttpPost("https://speedport.ip/index/login.cgi"); List - * nvps = new ArrayList(); nvps.add(new - * BasicNameValuePair("Username", "admin")); nvps.add(new - * BasicNameValuePair("Password", password)); httpPost.setEntity(new - * UrlEncodedFormEntity(nvps)); HttpResponse response2 = - * httpclient.execute(httpPost); - * - * try { System.out.println(response2.getStatusLine()); HttpEntity - * entity2 = response2.getEntity(); // do something useful with the - * response body // and ensure it is fully consumed - * EntityUtils.consume(entity2); } finally { - * httpPost.releaseConnection(); } - */ - - /* - * // Apache 2 PostMethod post = new - * PostMethod("https://speedport.ip/index/login.cgi"); NameValuePair[] - * data = { new NameValuePair("Username", "admin"), new - * NameValuePair("Password", password) }; post.setRequestBody(data); - * - * // post.setParameter("Cookie", "SessionID_R3=Rian5OU7Cuj"); - * post.addRequestHeader("Referer", "https://speedport.ip/"); - * post.addRequestHeader("Cookie", "SessionID_R3=Rian5OU7Cuj"); - * - * try { int resp = client.executeMethod(post); - * System.out.println("Response Code: " + resp); } catch (HttpException - * e1) { e1.printStackTrace(); } catch (IOException e1) { - * e1.printStackTrace(); } - * - * // execute method and handle any error responses. try { InputStream - * in = post.getResponseBodyAsStream(); System.out.println("response: " - * + in); } catch (IOException e) { e.printStackTrace(); } - */ - - httpclient.getConnectionManager().shutdown(); + + private void showStatus(URLConnection connection) throws IOException { + HttpURLConnection httpConnection = (HttpURLConnection) connection; + log.info("HTTP Status " + httpConnection.getResponseCode() + " - " + httpConnection.getURL()); } /**