-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlUnitExemplo.java
More file actions
114 lines (85 loc) · 4.33 KB
/
HtmlUnitExemplo.java
File metadata and controls
114 lines (85 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package logDownloader;
import java.io.File;
import java.net.Authenticator;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.FileUtils;
import Utils.CustomAuthenticator;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
//MAVEN DEPENDENCY
//<dependency>
//<groupId>net.sourceforge.htmlunit</groupId>
//<artifactId>htmlunit</artifactId>
//<version>2.23</version>
//</dependency>
public class HtmlUnitExemplo {
public void htmlUnitProxyExemplo(String username,
String password,
String workstation,
String domain,
String myproxyserver,
int myProxyPort,
String webPage
)throws Exception {
//Workstation normalmente é "localhost"
//Webpage = "http://uol.com.br" ou "http://192.168.0.1"
try (final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER, myproxyserver, myProxyPort)) {
//set proxy username and password
final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
//Autenticação do proxy
credentialsProvider.addNTLMCredentials(username, password, null, -1, workstation, domain);
//faz basic authentication se necessário
basicAuthentication(webClient, username, password);
//Javascript desligado -- IMPORTANTE!!!!!!
// A maioria dos scripts javascript dão erro pelo htmlunit
webClient.getOptions().setJavaScriptEnabled(false);
//PAGINA 1 - SELEÇÃO DE EQUIPE
System.out.println("PAGINA 01 \n---------");
//Conexão com a página
final HtmlPage page = webClient.getPage(webPage);
//String pageText = page.asText();
//System.out.println(pageText);
//Como o formulário é único na página, pegamos o form pelo ID através de get(0)
final HtmlForm form = page.getForms().get(0);
//Pegamos os atributos dos radio buttons através do nome "equipe" setado no input e printamos o Value
List<HtmlRadioButtonInput> radioButtonsEquipes = form.getRadioButtonsByName("equipe");
for (HtmlRadioButtonInput radioName : radioButtonsEquipes){
System.out.println(radioName.getValueAttribute().toString());
}
//Setamos um objeto HtmlRadioButtonInput atrávés do valor "Mackenzie"
final HtmlRadioButtonInput radioButton = form.getInputByValue("Mackenzie");
//Simulação de click do botão Mackenzie
radioButton.click();
final HtmlSubmitInput button = form.getInputByValue("Ver Tecnologias");
//Carrega outra página através do clique do botão ou pode ser através do clique de um link
final HtmlPage page2 = button.click();
System.out.println("==================================================================================================");
}
}
public void basicAuthentication(WebClient webClient, String username, String password) {
//BASIC AUTHENTICATION DA LI1630
DefaultCredentialsProvider basicAuth = new DefaultCredentialsProvider();
basicAuth.addCredentials(username, password);
webClient.setCredentialsProvider(basicAuth);
}
public String dataString() {
Date data = new Date();
SimpleDateFormat formatador = new SimpleDateFormat("yyyy.MM.dd");
return formatador.format( data );
}
//---------------------------------------------------------------------------------------------
//Autenticação para download de arquivo
public void downloadFile(URL url, File downloadedFile) throws Exception{
Authenticator.setDefault(new CustomAuthenticator());
FileUtils.copyURLToFile(url, downloadedFile);
System.out.println("LOG COPIADO : " + downloadedFile.getAbsolutePath().toString());
}
}