This commit is contained in:
2012-07-19 10:33:57 +02:00
parent 065cec6ddb
commit 5da979b312
2 changed files with 48 additions and 150 deletions
+20 -22
View File
@@ -1,23 +1,21 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>WebCallerID</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>WebCallerID</artifactId> <groupId>WebCallerID</groupId>
<version>0.0.1-SNAPSHOT</version> <artifactId>WebCallerID</artifactId>
<dependencies> <version>0.0.1-SNAPSHOT</version>
<dependency> <dependencies>
<groupId>ant</groupId> <dependency>
<artifactId>ant-apache-log4j</artifactId> <groupId>log4j</groupId>
<version>1.6.5</version> <artifactId>log4j</artifactId>
</dependency> <version>1.2.17</version>
<dependency> </dependency>
<groupId>org.apache.commons</groupId> <dependency>
<artifactId>commons-lang3</artifactId> <groupId>org.apache.commons</groupId>
<version>3.1</version> <artifactId>commons-lang3</artifactId>
</dependency> <version>3.0.1</version>
<dependency> <type>jar</type>
<groupId>org.apache.httpcomponents</groupId> <scope>compile</scope>
<artifactId>httpclient</artifactId> </dependency>
<version>4.2.1</version> </dependencies>
</dependency>
</dependencies>
</project> </project>
@@ -9,36 +9,23 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import org.apache.http.HttpEntity; import org.apache.log4j.BasicConfigurator;
import org.apache.http.HttpRequest; import org.apache.log4j.ConsoleAppender;
import org.apache.http.HttpResponse; import org.apache.log4j.Logger;
import org.apache.http.NameValuePair; import org.apache.log4j.PatternLayout;
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;
public class CallerIdClient { 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 password = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode("53795306".getBytes());
String cookie; String cookie;
public CallerIdClient() { public CallerIdClient() {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} - %m%n")));
// com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(null);
// plain Java
try { try {
login(); login();
getList(); getList();
@@ -46,20 +33,12 @@ public class CallerIdClient {
} catch (Exception e1) { } catch (Exception e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
// Apache http client
// try {
// apacheLogin();
// } catch (Exception e) {
// e.printStackTrace();
// }
} }
private void login() throws Exception { private void login() throws Exception {
System.out.println("--- login() --------------------------------------"); log.info("login()");
String body = "Username=" + URLEncoder.encode("admin", "UTF-8") + "&" + "Password=" + password; 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"); URL url2 = new URL("https://speedport.ip/index/login.cgi");
HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
@@ -92,9 +71,7 @@ public class CallerIdClient {
} }
private void getList() throws Exception { private void getList() throws Exception {
System.out.println("--- getList() --------------------------------------"); log.info("getList()");
//https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3,xctyNRvrwuh
URL u = new URL("https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie); URL u = new URL("https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie);
URLConnection urlConn = u.openConnection(); URLConnection urlConn = u.openConnection();
String cookieString = "SessionID_R3=" + cookie; String cookieString = "SessionID_R3=" + cookie;
@@ -106,104 +83,27 @@ public class CallerIdClient {
} }
private void logout() throws Exception { private void logout() throws Exception {
System.out.println("--- logout() --------------------------------------"); 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?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie);
URLConnection urlConn = myUrl.openConnection(); URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?cookie=SessionID_R3," + cookie);
String cookieString = "SessionID_R3=" + cookie; String cookieString = "SessionID_R3=" + cookie;
URLConnection urlConn = myUrl.openConnection();
urlConn.setRequestProperty("Cookie", cookieString); urlConn.setRequestProperty("Cookie", cookieString);
urlConn.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie);
urlConn.connect(); urlConn.connect();
showStatus(urlConn);
// URL u = new URL("https://speedport.ip/pub/top_beenden.php");
// // https://speedport.ip/logout.cgi?RequestFile=/pub/top_beenden.php myUrl = new URL("https://speedport.ip/");
// String r = new Scanner(u.openStream()).useDelimiter("\\Z").next(); urlConn = myUrl.openConnection();
// System.out.println("Logout:"); urlConn.setRequestProperty("Cookie", cookieString);
// System.out.println(r); urlConn.setRequestProperty("Referer", "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie);
urlConn.connect();
showStatus(urlConn);
} }
private void plainJava() throws Exception { private void showStatus(URLConnection connection) throws IOException {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
URL url = new URL("https://speedport.ip/"); log.info("HTTP Status " + httpConnection.getResponseCode() + " - " + httpConnection.getURL());
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<NameValuePair>
* nvps = new ArrayList<NameValuePair>(); 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();
} }
/** /**