sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to sun.net.www.protocol.http.HttpURLConnection

允我心安 提交于 2019-12-02 03:35:38

@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

Change your import statement for HttpURLConnection class from import sun.net.www.protocol.http.HttpURLConnection; to import java.net.HttpURLConnection;.

You are not supposed to use sun packages and classes in your application code. They are internal to JVM.

I was facing same issue because i imported

import com.sun.net.ssl.HttpsURLConnection;

remove that and import below

import javax.net.ssl.HttpsURLConnection;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!