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 {
httpclient.setRedirectStrategy(new DefaultRedirectStrategy() { login();
public boolean isRedirected(HttpRequest request, logout();
HttpResponse response, HttpContext context) { } catch (Exception e1) {
boolean isRedirect = false; e1.printStackTrace();
try { }
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) { }
// TODO Auto-generated catch block
e.printStackTrace(); private void login() throws Exception {
} System.out.println("Login:");
if (!isRedirect) { String body = "Username=" + URLEncoder.encode("admin", "UTF-8") + "&"
int responseCode = response.getStatusLine().getStatusCode(); + "Password=" + password;
if (responseCode == 301 || responseCode == 302) { System.out.println(body);
return true;
} URL url2 = new URL("https://speedport.ip/index/login.cgi");
} HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
return isRedirect; 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"); URL url = new URL("https://speedport.ip/");
// HttpResponse response1 = httpclient.execute(httpGet); URLConnection conn = url.openConnection();
// 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 { for (int i = 0;; i++) {
// System.out.println(response1.getStatusLine()); String name = conn.getHeaderFieldKey(i);
// HttpEntity entity1 = response1.getEntity(); String value = conn.getHeaderField(i);
// do something useful with the response body if (name == null && value == null) {
// and ensure it is fully consumed break;
// EntityUtils.consume(entity1); }
} finally { if (name == null) {
httpGet.releaseConnection(); 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"); // Startseite
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); URL u = new URL("https://speedport.ip/");
nvps.add(new BasicNameValuePair("username", "admin")); String r = new Scanner(u.openStream()).useDelimiter("\\Z").next();
nvps.add(new BasicNameValuePair("password", Arrays System.out.println(r);
.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() {
// 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.
*/
} }
/** /**