plain Java

This commit is contained in:
2012-07-17 16:13:19 +02:00
parent 38fd80af34
commit b17ce906b3
@@ -1,137 +1,123 @@
package de.tilman.callerId; package de.tilman.callerId;
import java.util.ArrayList; import java.io.BufferedReader;
import java.util.Arrays; import java.io.InputStreamReader;
import java.util.List; 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.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(); DefaultHttpClient httpclient = new DefaultHttpClient();
String password = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode("53795306".getBytes());
String cookie;
public CallerIdClient() { public CallerIdClient() {
// HttpGet httpget = new HttpGet("http://www.myspace.com/auth/form");
// com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(null); // 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 { try {
isRedirect = super.isRedirected(request, response, context); login();
} catch (ProtocolException e) { logout();
// TODO Auto-generated catch block } catch (Exception e1) {
e.printStackTrace(); e1.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return isRedirect;
}
});
try {
getTest();
} catch (Exception e) {
e.printStackTrace();
} }
httpclient.getConnectionManager().shutdown();
} }
private void getTest() throws Exception { private void login() throws Exception {
System.out.println("Login:");
String body = "Username=" + URLEncoder.encode("admin", "UTF-8") + "&"
+ "Password=" + password;
System.out.println(body);
HttpGet httpGet = new HttpGet("https://speedport.ip"); URL url2 = new URL("https://speedport.ip/index/login.cgi");
// HttpResponse response1 = httpclient.execute(httpGet); 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()));
// HttpGet httpget = new HttpGet("http://www.google.com/"); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
ResponseHandler<String> responseHandler = new BasicResponseHandler(); writer.write(body);
String responseBody = httpclient.execute(httpGet, responseHandler); writer.flush();
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
try { for (String line; (line = reader.readLine()) != null;) {
// System.out.println(response1.getStatusLine()); if (line.startsWith("SessionID_R3")) {
// HttpEntity entity1 = response1.getEntity(); System.out.println("Cookie: " + line);
// do something useful with the response body cookie = line.substring(13);
// and ensure it is fully consumed
// EntityUtils.consume(entity1);
} finally {
httpGet.releaseConnection();
} }
else {
System.out.println("Answer: " + line);
// -----
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", Arrays
.toString(org.apache.commons.codec.binary.Base64
.encodeBase64("53795306".getBytes()))));
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();
} }
} }
private void speedportLogin() { int status = connection.getResponseCode();
// client.getParams().setParameter("j_username", "abc"); System.out.println("Status: " + status);
// client.getParams().setParameter("j_password", "pqr");
/*
* PostMethod post = new writer.close();
* PostMethod("https://speedport.ip/index/login.cgi"); NameValuePair[] reader.close();
* data = { new NameValuePair("username", "admin"), new }
* NameValuePair("password",
* Arrays.toString(org.apache.commons.codec.binary private void getList() throws Exception {
* .Base64.encodeBase64("53795306".getBytes()))) }; // SessionID_R3=TGmoTck49mz
* post.setRequestBody(data);
* URL u = new URL("https://speedport.ip/");
* // post.setParameter("Cookie", "SessionID_R3=Rian5OU7Cuj"); String r = new Scanner(u.openStream()).useDelimiter("\\Z").next();
* post.addRequestHeader("Referer", "https://speedport.ip/"); System.out.println(r);
* post.addRequestHeader("Cookie", "SessionID_R3=Rian5OU7Cuj");
* }
* try { int resp = client.executeMethod(post);
* System.out.println("Response Code: " + resp); } catch (HttpException private void logout() throws Exception {
* e1) { e1.printStackTrace(); } catch (IOException e1) {
* e1.printStackTrace(); } URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie);
* URLConnection urlConn = myUrl.openConnection();
* // execute method and handle any error responses. try { InputStream String cookieString = "SessionID_R3=" + cookie;
* in = post.getResponseBodyAsStream(); System.out.println("response: " urlConn.setRequestProperty("Cookie", cookieString);
* + in); } catch (IOException e) { e.printStackTrace(); } // handle urlConn.connect();
* response.
*/ // 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 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);
} }
/** /**