diff --git a/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java b/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java index 3059b15..87d86c9 100755 --- a/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java +++ b/WebCallerID/src/main/java/de/tilman/callerId/CallerIdClient.java @@ -1,137 +1,123 @@ package de.tilman.callerId; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +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.ResponseHandler; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.BasicResponseHandler; 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 { DefaultHttpClient httpclient = new DefaultHttpClient(); + + String password = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode("53795306".getBytes()); + String cookie; public CallerIdClient() { - // HttpGet httpget = new HttpGet("http://www.myspace.com/auth/form"); // com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(null); - - - 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) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - if (!isRedirect) { - int responseCode = response.getStatusLine().getStatusCode(); - if (responseCode == 301 || responseCode == 302) { - return true; - } - } - return isRedirect; + + try { + login(); + logout(); + } catch (Exception e1) { + e1.printStackTrace(); + } + + } + + private void login() throws Exception { + System.out.println("Login:"); + String body = "Username=" + URLEncoder.encode("admin", "UTF-8") + "&" + + "Password=" + password; + System.out.println(body); + + URL url2 = new URL("https://speedport.ip/index/login.cgi"); + HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); + connection.setRequestMethod("POST"); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setUseCaches(false); + connection.setRequestProperty("Content-Length", String.valueOf(body.length())); + + OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); + writer.write(body); + writer.flush(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + for (String line; (line = reader.readLine()) != null;) { + if (line.startsWith("SessionID_R3")) { + System.out.println("Cookie: " + line); + cookie = line.substring(13); + } + else { + System.out.println("Answer: " + line); } - }); - - try { - getTest(); - } catch (Exception e) { - e.printStackTrace(); } - httpclient.getConnectionManager().shutdown(); + int status = connection.getResponseCode(); + System.out.println("Status: " + status); + + + writer.close(); + reader.close(); + } + + private void getList() throws Exception { + // SessionID_R3=TGmoTck49mz + + URL u = new URL("https://speedport.ip/"); + String r = new Scanner(u.openStream()).useDelimiter("\\Z").next(); + System.out.println(r); + + } + + private void logout() throws Exception { + + URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie); + URLConnection urlConn = myUrl.openConnection(); + String cookieString = "SessionID_R3=" + cookie; + urlConn.setRequestProperty("Cookie", cookieString); + 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); } - private void getTest() throws Exception { + private void plainJava() throws Exception { - HttpGet httpGet = new HttpGet("https://speedport.ip"); -// HttpResponse response1 = httpclient.execute(httpGet); - -// HttpGet httpget = new HttpGet("http://www.google.com/"); - ResponseHandler responseHandler = new BasicResponseHandler(); - String responseBody = httpclient.execute(httpGet, responseHandler); - System.out.println("----------------------------------------"); - System.out.println(responseBody); - System.out.println("----------------------------------------"); - + URL url = new URL("https://speedport.ip/"); + URLConnection conn = url.openConnection(); - try { -// System.out.println(response1.getStatusLine()); -// HttpEntity entity1 = response1.getEntity(); - // do something useful with the response body - // and ensure it is fully consumed -// EntityUtils.consume(entity1); - } finally { - httpGet.releaseConnection(); + 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); + } } - - - - // ----- - 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", Arrays - .toString(org.apache.commons.codec.binary.Base64 - .encodeBase64("53795306".getBytes())))); - httpPost.setEntity(new UrlEncodedFormEntity(nvps)); - HttpResponse response2 = httpclient.execute(httpPost); + // Startseite + URL u = new URL("https://speedport.ip/"); + String r = new Scanner(u.openStream()).useDelimiter("\\Z").next(); + System.out.println(r); - 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(); - } - } - - private void speedportLogin() { - // client.getParams().setParameter("j_username", "abc"); - // client.getParams().setParameter("j_password", "pqr"); - /* - * PostMethod post = new - * PostMethod("https://speedport.ip/index/login.cgi"); NameValuePair[] - * data = { new NameValuePair("username", "admin"), new - * NameValuePair("password", - * Arrays.toString(org.apache.commons.codec.binary - * .Base64.encodeBase64("53795306".getBytes()))) }; - * 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(); } // handle - * response. - */ } /**