BAEL-5741 Download a Webpage in Java (#13531)

* BAEL-5741 Download a Webpage in Java

* BAEL-5741 Download a Webpage in Java

* BAEL-5741 Download a Webpage in Java

* BAEL-5741 Download a Webpage in Java

* BAEL-5741 Download a Webpage in Java
This commit is contained in:
Michael Olayemi 2023-02-26 04:26:44 +01:00 committed by GitHub
parent a713d96d4d
commit 41b3460af6
2 changed files with 55 additions and 0 deletions

View File

@ -20,6 +20,12 @@
<artifactId>commons-validator</artifactId>
<version>${apache.commons-validator.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
</dependencies>
<build>
@ -28,6 +34,7 @@
<properties>
<apache.commons-validator.version>1.7</apache.commons-validator.version>
<jsoup.version>1.15.4</jsoup.version>
</properties>
</project>

View File

@ -0,0 +1,48 @@
package com.baeldung.downloadwebpage;
import static org.junit.jupiter.api.Assertions.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.Test;
class DownloadWebpageUnitTest {
@Test
public void givenURLConnection_whenRetrieveWebpage_thenWebpageIsNotNullAndContainsHtmlTag() throws IOException {
URL url = new URL("https://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
}
assertNotNull(responseBuilder);
assertTrue(responseBuilder.toString()
.contains("<html>"));
}
}
@Test
public void givenJsoup_whenRetrievingWebpage_thenWebpageDocumentIsNotNullAndContainsHtmlTag() throws IOException {
Document document = Jsoup.connect("https://example.com")
.get();
String webpage = document.html();
assertNotNull(webpage);
assertTrue(webpage.contains("<html>"));
}
}