Guest User

Untitled

a guest
Dec 18th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. import java.net.URI;
  2. import java.net.URLEncoder;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.apache.http.Header;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpHost;
  9. import org.apache.http.NameValuePair;
  10. import org.apache.http.client.CookieStore;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.client.protocol.ClientContext;
  15. import org.apache.http.client.protocol.HttpClientContext;
  16. import org.apache.http.client.utils.URIUtils;
  17. import org.apache.http.cookie.Cookie;
  18. import org.apache.http.client.methods.HttpGet;
  19. import org.apache.http.impl.client.BasicCookieStore;
  20. import org.apache.http.impl.client.CloseableHttpClient;
  21. import org.apache.http.impl.client.HttpClientBuilder;
  22. import org.apache.http.impl.client.HttpClients;
  23. import org.apache.http.impl.cookie.BasicClientCookie;
  24. import org.apache.http.message.BasicNameValuePair;
  25. import org.apache.http.protocol.BasicHttpContext;
  26. import org.apache.http.protocol.HttpContext;
  27. import org.apache.http.util.EntityUtils;
  28. import org.jsoup.Jsoup;
  29. import org.jsoup.nodes.Document;
  30.  
  31. public class Login {
  32.     @SuppressWarnings("deprecation")
  33.     public static String loginAndGetHTML() throws Exception {
  34.        
  35.         // Create a local instance of cookie store
  36.         CookieStore cookieStore = new BasicCookieStore();
  37.        
  38.        
  39.  
  40.         CloseableHttpClient httpclient = HttpClients.createDefault();
  41.         HttpClientContext context = HttpClientContext.create();
  42.        
  43.    
  44.         // Bind custom cookie store to the local context
  45.         context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
  46.        
  47.         String auth_token;
  48.         String html;
  49.         CloseableHttpResponse response;
  50.         String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
  51.        
  52.         // Setze Nutzernamen und Password
  53.         String username = URLEncoder.encode("jody.friedrich@gmx.de", "UTF-8");
  54.         String password = "Knappel99";
  55.        
  56.         String currentURL;
  57.        
  58.         HttpGet HttpGet = new HttpGet("https://profile.ea.com/");
  59.        
  60.         HttpGet.addHeader("User-Agent", userAgent);
  61.         HttpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  62.         HttpGet.addHeader("Accept-Language", "de,en-US;q=0.7,en;q=0.3");
  63.         HttpGet.addHeader("Accept-Encoding", "gzip, deflate, br");
  64.         HttpGet.addHeader("Connection", "keep-alive");
  65.         HttpGet.addHeader("Cache-Control", "max-age=0");
  66.  
  67.        
  68.        
  69.         response = httpclient.execute(HttpGet, context);
  70.        
  71.        
  72.         // Nach weiterleitungen letzte URL auslesen
  73.        
  74.         HttpHost target = context.getTargetHost();
  75.         List<URI> redirectLocations = context.getRedirectLocations();
  76.         URI location = URIUtils.resolve(HttpGet.getURI(), target, redirectLocations);
  77.         currentURL = location.toASCIIString();
  78.        
  79.  
  80.  
  81.                
  82.        
  83.         //
  84.            
  85.         //System.out.println(currentURL);
  86.        
  87.        
  88.        
  89.          // Versuche einzuloggen
  90.          
  91.         /*HttpPost httpPost = new HttpPost(currentURL);
  92.         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  93.         nvps.add(new BasicNameValuePair("email", username));
  94.         nvps.add(new BasicNameValuePair("password", password));
  95.        
  96.         nvps.add(new BasicNameValuePair("_rememberMe", "on"));
  97.         nvps.add(new BasicNameValuePair("rememberMe", "on"));
  98.         nvps.add(new BasicNameValuePair("_eventId", "submit"));
  99.         nvps.add(new BasicNameValuePair("gCaptchaResponse", ""));
  100.        
  101.        
  102.         httpPost.setEntity(new UrlEncodedFormEntity(nvps));
  103.         httpPost.addHeader("Referer", currentURL);
  104.         httpPost.addHeader("User-Agent", userAgent);
  105.         httpPost.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8");
  106.         httpPost.addHeader("Accept-Language", "de,en-US;q=0.7,en;q=0.3");
  107.         httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
  108.         httpPost.addHeader("Connection", "keep-alive");
  109.         httpPost.addHeader("Cache-Control", "max-age=0");
  110.         CloseableHttpResponse response1 = httpclient.execute(httpPost);
  111.         */
  112.        
  113.        
  114.        
  115.        
  116.        
  117.        
  118.         try {
  119.             HttpEntity entity = response.getEntity();
  120.             html = EntityUtils.toString(entity);
  121.             EntityUtils.consume(entity);
  122.         } finally {
  123.             response.close();
  124.             //response1.close();
  125.         }
  126.        
  127.         if(html.contains("Falsche Nutzerdaten")) {
  128.             throw new Exception("Login fehlgeschlagen");
  129.         }
  130.        
  131.        
  132.        
  133.         return html;
  134.     }
  135.  
  136.     public static String parseHTML(String html) throws Exception {
  137.         Document doc = Jsoup.parse(html);
  138.         String zahl = doc.getElementById("zahl").text();
  139.         return zahl;
  140.     }
  141.  
  142. }
Add Comment
Please, Sign In to add comment