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);
try {
login();
logout();
} catch (Exception e1) {
e1.printStackTrace();
}
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() { }
public boolean isRedirected(HttpRequest request,
HttpResponse response, HttpContext context) { private void login() throws Exception {
boolean isRedirect = false; System.out.println("Login:");
try { String body = "Username=" + URLEncoder.encode("admin", "UTF-8") + "&"
isRedirect = super.isRedirected(request, response, context); + "Password=" + password;
} catch (ProtocolException e) { System.out.println(body);
// TODO Auto-generated catch block
e.printStackTrace(); URL url2 = new URL("https://speedport.ip/index/login.cgi");
} HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
if (!isRedirect) { connection.setRequestMethod("POST");
int responseCode = response.getStatusLine().getStatusCode(); connection.setDoInput(true);
if (responseCode == 301 || responseCode == 302) { connection.setDoOutput(true);
return true; connection.setUseCaches(false);
} connection.setRequestProperty("Content-Length", String.valueOf(body.length()));
}
return isRedirect; 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 getTest() throws Exception { private void getList() throws Exception {
// SessionID_R3=TGmoTck49mz
HttpGet httpGet = new HttpGet("https://speedport.ip"); URL u = new URL("https://speedport.ip/");
// HttpResponse response1 = httpclient.execute(httpGet); String r = new Scanner(u.openStream()).useDelimiter("\\Z").next();
System.out.println(r);
// HttpGet httpget = new HttpGet("http://www.google.com/");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpGet, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
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();
}
// -----
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() { private void logout() throws Exception {
// client.getParams().setParameter("j_username", "abc");
// client.getParams().setParameter("j_password", "pqr"); URL myUrl = new URL("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie);
/* URLConnection urlConn = myUrl.openConnection();
* PostMethod post = new String cookieString = "SessionID_R3=" + cookie;
* PostMethod("https://speedport.ip/index/login.cgi"); NameValuePair[] urlConn.setRequestProperty("Cookie", cookieString);
* data = { new NameValuePair("username", "admin"), new urlConn.connect();
* NameValuePair("password",
* Arrays.toString(org.apache.commons.codec.binary // URL u = new URL("https://speedport.ip/pub/top_beenden.php");
* .Base64.encodeBase64("53795306".getBytes()))) }; // // https://speedport.ip/logout.cgi?RequestFile=/pub/top_beenden.php
* post.setRequestBody(data); // String r = new Scanner(u.openStream()).useDelimiter("\\Z").next();
* // System.out.println("Logout:");
* // post.setParameter("Cookie", "SessionID_R3=Rian5OU7Cuj"); // System.out.println(r);
* post.addRequestHeader("Referer", "https://speedport.ip/"); }
* post.addRequestHeader("Cookie", "SessionID_R3=Rian5OU7Cuj");
* private void plainJava() throws Exception {
* try { int resp = client.executeMethod(post);
* System.out.println("Response Code: " + resp); } catch (HttpException URL url = new URL("https://speedport.ip/");
* e1) { e1.printStackTrace(); } catch (IOException e1) { URLConnection conn = url.openConnection();
* e1.printStackTrace(); }
* for (int i = 0;; i++) {
* // execute method and handle any error responses. try { InputStream String name = conn.getHeaderFieldKey(i);
* in = post.getResponseBodyAsStream(); System.out.println("response: " String value = conn.getHeaderField(i);
* + in); } catch (IOException e) { e.printStackTrace(); } // handle if (name == null && value == null) {
* response. 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);
} }
/** /**