package de.kamelstall.modsde.comm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import android.util.Log;
/**
* This class does nothing more, than waiting for a response from a request to
* the misc.php. Once received the source code is parsed and the
* mods.de/setcookie.php file is called.
*/
public class LoginPageHandler implements ResponseListener {
public static String url(String username, String password) throws UnsupportedEncodingException {
return MdeStatus.URL_LOGIN
+ "&login_username=" + URLEncoder.encode(username, MdeStatus.DEFAULT_ENCODING)
+ "&login_password=" + URLEncoder.encode(password, MdeStatus.DEFAULT_ENCODING);
}
@Override
public void onResponseReceived(HttpResponse response) {
try {
try {
String encoding = MdeStatus.DEFAULT_ENCODING;
if (response.getEntity().getContentEncoding() != null) {
encoding = response.getEntity().getContentEncoding().getValue();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), encoding));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
String input = sb.toString();
// search for setcookie.php
if (input.contains("setcookie.php")) {
Pattern pattern = Pattern.compile("http://mods.de/setcookie[^\"]*");
Matcher m = pattern.matcher(input);
if (m.find()) {
Log.d("MDE", "Found: " + m.group());
// send request to set cookies.
HTTPClient.sendRequest(m.group(), null);
}
}
else {
// "Wrong login :("
}
} finally {
// indicate, that content has been consumed
response.getEntity().consumeContent();
}
} catch (IOException e) {
}
}
}
|