Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.URI;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.Header;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpHost;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.CookieStore;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.protocol.HttpClientContext;
- import org.apache.http.client.utils.URIUtils;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.BasicCookieStore;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- public class Login {
- public static String loginAndGetHTML() throws Exception {
- HttpClient http = null;
- CookieStore httpCookieStore = new BasicCookieStore();
- HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore);
- http = builder.build();
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpClientContext context = HttpClientContext.create();
- String html;
- CloseableHttpResponse response;
- String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
- // Setze Nutzernamen und Password
- String username = URLEncoder.encode("jody.friedrich@gmx.de", "UTF-8");
- String password = "Knappel99";
- String currentURL;
- HttpGet HttpGet = new HttpGet("https://profile.ea.com/");
- HttpGet.addHeader("User-Agent", userAgent);
- response = httpclient.execute(HttpGet, context);
- // Nach weiterleitungen letzte URL auslesen
- HttpHost target = context.getTargetHost();
- List<URI> redirectLocations = context.getRedirectLocations();
- URI location = URIUtils.resolve(HttpGet.getURI(), target, redirectLocations);
- currentURL = location.toASCIIString();
- System.out.println(httpCookieStore.getCookies());
- //response.close();
- //System.out.println(currentURL);
- /*// Versuche einzuloggen
- HttpPost httpPost = new HttpPost(currentURL);
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- nvps.add(new BasicNameValuePair("email", username));
- nvps.add(new BasicNameValuePair("password", password));
- nvps.add(new BasicNameValuePair("_rememberMe", "on"));
- nvps.add(new BasicNameValuePair("rememberMe", "on"));
- nvps.add(new BasicNameValuePair("_eventId", "submit"));
- nvps.add(new BasicNameValuePair("gCaptchaResponse", ""));
- httpPost.setEntity(new UrlEncodedFormEntity(nvps));
- httpPost.addHeader("Referer", currentURL);
- httpPost.addHeader("User-Agent", userAgent);
- CloseableHttpResponse response1 = httpclient.execute(httpPost);
- */
- try {
- HttpEntity entity = response.getEntity();
- html = EntityUtils.toString(entity);
- EntityUtils.consume(entity);
- } finally {
- response.close();
- }
- if(html.contains("Falsche Nutzerdaten")) {
- throw new Exception("Login fehlgeschlagen");
- }
- return html;
- }
- public static String parseHTML(String html) throws Exception {
- Document doc = Jsoup.parse(html);
- String zahl = doc.getElementById("zahl").text();
- return zahl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement