Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.Arrays;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. import org.apache.commons.codec.binary.Base64;
  6. import org.apache.http.auth.AuthScope;
  7. import org.apache.http.auth.UsernamePasswordCredentials;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.CredentialsProvider;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.BasicCredentialsProvider;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.protocol.BasicHttpContext;
  16. import org.apache.http.protocol.HttpContext;
  17. import org.apache.http.util.EntityUtils;
  18. import org.openqa.selenium.By;
  19. import org.openqa.selenium.WebDriver;
  20. import org.openqa.selenium.WebElement;
  21. import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  22. import org.openqa.selenium.support.ui.ExpectedConditions;
  23. import org.openqa.selenium.support.ui.WebDriverWait;
  24.  
  25.  
  26. public class SiteConnector {
  27.  
  28. private static String username = "perbro";
  29. private static String password = "9704";
  30. private static String getDistrictURL = "https://app.di.no/app/play/edg/";
  31.  
  32. public static void main(String[] args) {
  33.  
  34. new SiteConnector().connect();
  35. }
  36.  
  37. public void connect() {
  38. // Create a new instance of the html unit driver
  39. // Notice that the remainder of the code relies on the interface,
  40. // not the implementation.
  41. WebDriver driver = new HtmlUnitDriver();
  42.  
  43. // And now use this to visit Google
  44. driver.get(getDistrictURL);
  45.  
  46. // Enter userd id
  47. WebElement element = driver.findElement(By.name("0"));
  48. element.sendKeys(username);
  49.  
  50. //wait 5 secs for userid to be entered
  51. driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  52.  
  53. //Enter Password
  54. WebElement element1 = driver.findElement(By.id("Lösenord"));
  55. element1.sendKeys(password);
  56.  
  57. //Submit button
  58. element.submit();
  59.  
  60. WebElement myDynamicElement = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.id("Logga in")));
  61. driver.findElement(By.id("Logga in")).click();
  62.  
  63. //press signout button
  64. driver.findElement(By.id("Logga in")).click();
  65.  
  66. }
  67.  
  68. public void login () {
  69. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  70. credsProvider.setCredentials(
  71. new AuthScope("https://app.di.no/app", 80),
  72. new UsernamePasswordCredentials("username", "password"));
  73. CloseableHttpClient httpclient = HttpClients.custom()
  74. .setDefaultCredentialsProvider(credsProvider)
  75. .build();
  76.  
  77. String enc = username + ":" + password;
  78. try {
  79. HttpGet httpget = new HttpGet("https://app.di.no/app/play/edg/edg/info?_edgForm.selectedRouteId=99971&_edgForm.selectedPendingRoute=20404-100136&_edgForm.selectedDateString=10.06.2016&_edgForm.mode=Normal&_edgForm.selectedLanguageCode=sv&_edgForm.showDirectives=true&_edgForm.showIfEmpty=false");
  80. httpget.addHeader("Authorization", "Basic " + Base64.encodeBase64String(enc.getBytes()));
  81.  
  82. System.out.println("Executing request " + httpget.getRequestLine());
  83. CloseableHttpResponse response = httpclient.execute(httpget);
  84. try {
  85. System.out.println("----------------------------------------");
  86. System.out.println(response.getStatusLine());
  87. System.out.println(EntityUtils.toString(response.getEntity()));
  88. } finally {
  89. response.close();
  90. }
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. } finally {
  94. try {
  95. httpclient.close();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement