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.ClientContext;
- import org.apache.http.client.protocol.HttpClientContext;
- import org.apache.http.client.utils.URIUtils;
- import org.apache.http.cookie.Cookie;
- 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.impl.cookie.BasicClientCookie;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.BasicHttpContext;
- import org.apache.http.protocol.HttpContext;
- import org.apache.http.util.EntityUtils;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- public class Login {
- @SuppressWarnings("deprecation")
- public static String loginAndGetHTML() throws Exception {
- // Create a local instance of cookie store
- CookieStore cookieStore = new BasicCookieStore();
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpClientContext context = HttpClientContext.create();
- // Bind custom cookie store to the local context
- context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
- String auth_token;
- 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);
- HttpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
- HttpGet.addHeader("Accept-Language", "de,en-US;q=0.7,en;q=0.3");
- HttpGet.addHeader("Accept-Encoding", "gzip, deflate, br");
- HttpGet.addHeader("Connection", "keep-alive");
- HttpGet.addHeader("Cache-Control", "max-age=0");
- 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(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);
- httpPost.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8");
- httpPost.addHeader("Accept-Language", "de,en-US;q=0.7,en;q=0.3");
- httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
- httpPost.addHeader("Connection", "keep-alive");
- httpPost.addHeader("Cache-Control", "max-age=0");
- CloseableHttpResponse response1 = httpclient.execute(httpPost);
- */
- try {
- HttpEntity entity = response.getEntity();
- html = EntityUtils.toString(entity);
- EntityUtils.consume(entity);
- } finally {
- response.close();
- //response1.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;
- }
- }
Add Comment
Please, Sign In to add comment