sun.net.www.protocol.https.HttpsURLConnectionImpl无法转换为sun.net.www.protocol.http.HttpURLConnection [英] sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to sun.net.www.protocol.http.HttpURLConnection
问题描述
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
@KarolDowbecki的分析方向正确.
而不是sun.net.www.protocol.http.HttpURLConnection;
HttpURLConnection 应该通过以下方式解决:
import java.net.HttpURLConnection;
我能够执行您的程序,并产生以下输出:
size of all link and images list81
https://makemysushi.com/404#navigation
https://makemysushi.com/
https://makemysushi.com/
https://makemysushi.com/sushi-university
https://makemysushi.com/sushi-recipes
https://makemysushi.com/sushi-essentials
https://makemysushi.com/store
https://www.facebook.com/Makemysushi/
https://www.instagram.com/explore/tags/makemysushi/
https://plus.google.com/+Makemysushi
mailto:info@makemysushi.com // <-- this href attribute is of MailToURLConnection type raising the java.lang.ClassCastException which can't be casted to HttpURLConnection type
.
.
.
https://makemysushi.com/Sushi-share/contact-us
https://makemysushi.com/Sushi-share/about-us
null
null
null
null
size of activelink list77
https://makemysushi.com/404#navigation----Not Found
https://makemysushi.com/----OK
https://makemysushi.com/----OK
https://makemysushi.com/sushi-university----OK
https://makemysushi.com/sushi-recipes----OK
https://makemysushi.com/sushi-essentials----OK
https://makemysushi.com/store----OK
https://www.facebook.com/Makemysushi/----OK
https://www.instagram.com/explore/tags/makemysushi/----OK
https://plus.google.com/+Makemysushi----OK
Exception in thread "main" java.lang.ClassCastException: sun.net.www.protocol.mailto.MailToURLConnection cannot be cast to java.net.HttpURLConnection
at demo.TestMakeMySushi.main(TestMakeMySushi.java:48)
原因
列表 活动列表包含一个元素:
<a href="mailto:info@makemysushi.com" target="_blank"><i id="social-em" class="fa fa-envelope-square fa-3x social"></i></a>
当您尝试通过 href 属性 mailto:info@makemysushi.com 建立连接时,如下所示:
HttpURLConnection connection = (HttpURLConnection) new URL(activelist.get(j).getAttribute("href")).openConnection();
无法建立成功的连接,因为无法将 MailToURLConnection 对象强制转换为 HttpURLConnection 对象并引发 java.lang.ClassCastException >
i am using selenium library for testing purpose. But the following code giving me class cast exception. i have googled this exception but didn't get the solution. i am confused in Https connetion and http connetion. help me to solve this exception. thank you
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 that i am facing
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
@KarolDowbecki's analysis was in the right direction.
Instead of sun.net.www.protocol.http.HttpURLConnection;
HttpURLConnection should be resolved through:
import java.net.HttpURLConnection;
I was able to execute your program which produces the following output:
size of all link and images list81
https://makemysushi.com/404#navigation
https://makemysushi.com/
https://makemysushi.com/
https://makemysushi.com/sushi-university
https://makemysushi.com/sushi-recipes
https://makemysushi.com/sushi-essentials
https://makemysushi.com/store
https://www.facebook.com/Makemysushi/
https://www.instagram.com/explore/tags/makemysushi/
https://plus.google.com/+Makemysushi
mailto:info@makemysushi.com // <-- this href attribute is of MailToURLConnection type raising the java.lang.ClassCastException which can't be casted to HttpURLConnection type
.
.
.
https://makemysushi.com/Sushi-share/contact-us
https://makemysushi.com/Sushi-share/about-us
null
null
null
null
size of activelink list77
https://makemysushi.com/404#navigation----Not Found
https://makemysushi.com/----OK
https://makemysushi.com/----OK
https://makemysushi.com/sushi-university----OK
https://makemysushi.com/sushi-recipes----OK
https://makemysushi.com/sushi-essentials----OK
https://makemysushi.com/store----OK
https://www.facebook.com/Makemysushi/----OK
https://www.instagram.com/explore/tags/makemysushi/----OK
https://plus.google.com/+Makemysushi----OK
Exception in thread "main" java.lang.ClassCastException: sun.net.www.protocol.mailto.MailToURLConnection cannot be cast to java.net.HttpURLConnection
at demo.TestMakeMySushi.main(TestMakeMySushi.java:48)
Reason
The List activelist contains an element:
<a href="mailto:info@makemysushi.com" target="_blank"><i id="social-em" class="fa fa-envelope-square fa-3x social"></i></a>
When you are trying to establish a connection through the href attribute mailto:info@makemysushi.com as in:
HttpURLConnection connection = (HttpURLConnection) new URL(activelist.get(j).getAttribute("href")).openConnection();
A successful connection cannot be established as MailToURLConnection object can't be casted to HttpURLConnection object and raises java.lang.ClassCastException
这篇关于sun.net.www.protocol.https.HttpsURLConnectionImpl无法转换为sun.net.www.protocol.http.HttpURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!