Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 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.HttpClientContext;
  15. import org.apache.http.client.utils.URIUtils;
  16. import org.apache.http.client.methods.HttpGet;
  17. import org.apache.http.impl.client.BasicCookieStore;
  18. import org.apache.http.impl.client.CloseableHttpClient;
  19. import org.apache.http.impl.client.HttpClientBuilder;
  20. import org.apache.http.impl.client.HttpClients;
  21. import org.apache.http.message.BasicNameValuePair;
  22. import org.apache.http.util.EntityUtils;
  23. import org.jsoup.Jsoup;
  24. import org.jsoup.nodes.Document;
  25.  
  26. public class Login {
  27.    
  28.     public static String loginAndGetHTML() throws Exception {
  29.        
  30.         HttpClient http = null;
  31.         CookieStore httpCookieStore = new BasicCookieStore();
  32.         HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore);
  33.         http = builder.build();
  34.        
  35.         CloseableHttpClient httpclient = HttpClients.createDefault();
  36.         HttpClientContext context = HttpClientContext.create();
  37.         String html;
  38.         CloseableHttpResponse response;
  39.         String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
  40.        
  41.         // Setze Nutzernamen und Password
  42.         String username = URLEncoder.encode("jody.friedrich@gmx.de", "UTF-8");
  43.         String password = "Knappel99";
  44.        
  45.         String currentURL;
  46.        
  47.         HttpGet HttpGet = new HttpGet("https://profile.ea.com/");
  48.         HttpGet.addHeader("User-Agent", userAgent);
  49.         response = httpclient.execute(HttpGet, context);
  50.        
  51.         // Nach weiterleitungen letzte URL auslesen
  52.        
  53.         HttpHost target = context.getTargetHost();
  54.         List<URI> redirectLocations = context.getRedirectLocations();
  55.         URI location = URIUtils.resolve(HttpGet.getURI(), target, redirectLocations);
  56.         currentURL = location.toASCIIString();
  57.        
  58.         System.out.println(httpCookieStore.getCookies());
  59.        
  60.         //response.close();
  61.            
  62.         //System.out.println(currentURL);
  63.        
  64.        
  65.         /*// Versuche einzuloggen
  66.         HttpPost httpPost = new HttpPost(currentURL);
  67.         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  68.         nvps.add(new BasicNameValuePair("email", username));
  69.         nvps.add(new BasicNameValuePair("password", password));
  70.        
  71.         nvps.add(new BasicNameValuePair("_rememberMe", "on"));
  72.         nvps.add(new BasicNameValuePair("rememberMe", "on"));
  73.         nvps.add(new BasicNameValuePair("_eventId", "submit"));
  74.         nvps.add(new BasicNameValuePair("gCaptchaResponse", ""));
  75.        
  76.        
  77.         httpPost.setEntity(new UrlEncodedFormEntity(nvps));
  78.         httpPost.addHeader("Referer", currentURL);
  79.         httpPost.addHeader("User-Agent", userAgent);
  80.         CloseableHttpResponse response1 = httpclient.execute(httpPost);
  81.         */
  82.        
  83.        
  84.        
  85.        
  86.        
  87.         try {
  88.             HttpEntity entity = response.getEntity();
  89.             html = EntityUtils.toString(entity);
  90.             EntityUtils.consume(entity);
  91.         } finally {
  92.             response.close();
  93.         }
  94.        
  95.         if(html.contains("Falsche Nutzerdaten")) {
  96.             throw new Exception("Login fehlgeschlagen");
  97.         }
  98.        
  99.        
  100.        
  101.         return html;
  102.     }
  103.  
  104.     public static String parseHTML(String html) throws Exception {
  105.         Document doc = Jsoup.parse(html);
  106.         String zahl = doc.getElementById("zahl").text();
  107.         return zahl;
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement