gpt4 book ai didi

java - sun.net.www.protocol.https.HttpsURLConnectionImpl 无法转换为 sun.net.www.protocol.http.HttpURLConnection

转载 作者:行者123 更新时间:2023-12-02 01:16:53 31 4
gpt4 key购买 nike

我正在使用 selenium 库进行测试。但下面的代码给了我类转换异常。我已经用谷歌搜索了这个异常,但没有得到解决方案。我对 Https 连接和 http 连接感到困惑。帮我解决这个异常。谢谢

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import sun.net.www.protocol.http.HttpURLConnection;


public class TestMakeMySushi {

private static final String HttpURLConnection = null;

public static void main(String[] args) throws InterruptedException, MalformedURLException, IOException {

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dev-24\\Downloads\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("https://www.makemysushi.com/404?");

List<WebElement> linklist = driver.findElements(By.tagName("a"));
linklist.addAll(driver.findElements(By.tagName("img")));
System.out.println("size of all link and images list" + linklist.size());
List<WebElement> activelist = new ArrayList<WebElement>();

for (int i = 0; i < linklist.size(); i++) {
System.out.println(linklist.get(i).getAttribute("href"));
if (linklist.get(i).getAttribute("href") != null && (!linklist.get(i).getAttribute("href").contains("javascript"))) {
activelist.add(linklist.get(i));
}
}


System.out.println("size of activelink list" + activelist.size());


for (int j = 0; j < activelist.size(); j++) {

HttpURLConnection connection = (HttpURLConnection) new URL(activelist.get(j).getAttribute("href")).openConnection();
connection.connect();
String response = connection.getResponseMessage();
connection.disconnect();
System.out.println(activelist.get(j).getAttribute("href") + "----" + response);
}
}
}

我面临的异常

Exception in thread "main" java.lang.ClassCastException:   sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to sun.net.www.protocol.http.HttpURLConnection
at TestMakeMySushi.main(TestMakeMySushi.java:76)
C:\Users\Dev-24\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1

最佳答案

HttpURLConnection 类的导入语句从 import sun.net.www.protocol.http.HttpURLConnection; 更改为 import java.net.HttpURLConnection;

您不应该在应用程序代码中使用 sun 包和类。它们是 JVM 内部的。

关于java - sun.net.www.protocol.https.HttpsURLConnectionImpl 无法转换为 sun.net.www.protocol.http.HttpURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57655925/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com