java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl cannot be cast to javax.net.ssl.HttpsURLConnection

纵饮孤独 提交于 2019-11-30 17:39:49

The solution is to change this line

URL url = new URL("https://redmine.xxx.cz/time_entries.xml");

into this line

URL url = new URL(null, "https://redmine.xxx.cz/time_entries.xml", new sun.net.www.protocol.https.Handler());

If you are using simply HTTP protocol (not HTTPS), instead of:

HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection();

Use this:

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

Just drop the "s" in "HttpsURLConnection"

Note that this solution works only for HTTP protocol (not HTTPS)

It called if url starts with "https"

    if(url.startswith("https"){ HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection(); }

use netty-3.9.2 > .jar to solve this issue When you use this netty.jar. the process of post call changed.

http://dl.bintray.com/netty/downloads/netty-3.9.12.Final.tar.bz2

To avoid this kind of error you can use HttpUrlConnection (java.net.HttpUrlConnection) for both http and https url

HttpsUrlConnection only add extra methods that "can be used" for https connection but you probably won't use these methods.

Both HttpsUrlConnection and HttpUrlConnection derived from UrlConnection which contains methods that permit to read the response like getInputStream(). HttpsUrlConnection derived from HttpUrlConnection which derived from UrlConnection.

java.net.HttpUrlConnection just add methods relative to http protocol (these methods apply to https)

https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/HttpsURLConnection.html

Thus you can do this :

URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

As well you can do this and obtain the same result, the connection will be the same :

URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!