'selenium - java - finding broken links

  import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.sql.Driver;
import java.time.Duration;  




 System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
            WebDriver driver = new ChromeDriver();
    
            driver.get("https://demoqa.com/");
            driver.manage().window().maximize();
    
            HttpClient client = HttpClientBuilder.create().build();  
    
            HttpGet request = new HttpGet("https://demoqa.com/"); 
            HttpResponse response = client.execute(request);    
    
            int statusCode = response.statusCode();
            System.out.println(statusCode);

hi, first of all, my first question is when I create the client object with HttpClient, for HttpClient it throws an error and says "Required type: HttpClient Provided: It gives a warning like "CloseableHttpClient".

Secondly The execute method in HttpClient is not working. Can you help me ?



Solution 1:[1]

You're mixing apples with pears, org.apache.http with java.net.http.

Maven dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

Code sample:

package tests;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class HttpClientTest {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpClient client = HttpClientBuilder.create().build();  
        HttpGet request = new HttpGet("https://demoqa.com/"); 
        HttpResponse response = client.execute(request);   
        System.out.println(response.getStatusLine());
        System.out.println(response.getStatusLine().getStatusCode());
    }

}

Output:

HTTP/1.1 200 OK
200

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1