-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlUnitProxyExemplo.java
More file actions
55 lines (44 loc) · 1.81 KB
/
HtmlUnitProxyExemplo.java
File metadata and controls
55 lines (44 loc) · 1.81 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
package logDownloader;
import java.io.File;
import java.net.Authenticator;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import Utils.CustomAuthenticator;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
//MAVEN DEPENDENCY
//<dependency>
//<groupId>net.sourceforge.htmlunit</groupId>
//<artifactId>htmlunit</artifactId>
//<version>2.23</version>
//</dependency>
public class HtmlUnitProxyExemplo {
public void homePage() throws Exception {
try (final WebClient webClient = new WebClient()) {
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
final String pageAsXml = page.asXml();
final String pageAsText = page.asText();
}
}
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());
}
}