java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl 无法转换为 javax.net.ssl.HttpsURL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23517139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:29:29  来源:igfitidea点击:

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

javaeclipsemavennetbeansclassnotfoundexception

提问by libik

I tried this code in my Netbeans 7.4 and it works without problem

我在我的 Netbeans 7.4 中尝试了这段代码,它没有问题

import java.io.IOException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class JavaApplication148 {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://redmine.ackee.cz/time_entries.xml");
        HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection();
    }
}

However then I used it in eclipse in my maven-driven project and it throws following exception :

但是后来我在我的 Maven 驱动的项目中的 Eclipse 中使用了它,它引发了以下异常:

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

This is following class in my maven project that throws an error

这是我的 Maven 项目中引发错误的课程

package cz.ackee.redmine.commands;

import java.io.IOException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public abstract class RedmineCommand {      
    public void write(String xmlAddress, String text, String requestMethod) throws IOException{         
         URL url = new URL("https://redmine.xxx.cz/time_entries.xml");
         HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection(); //this line throws exception
    }       
}

This is my pom.xml

这是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cz.ackee</groupId>
    <artifactId>pan-unicorn-bot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Pan Unicorn Bot</name>
    <description>Pan unicorn IRC Bot</description>

  <repositories>
  <repository>
    <id>apache-snapshots</id>
    <url>http://repository.apache.org/snapshots/</url>
  </repository>
  </repositories>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.3-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.taskadapter</groupId>
            <artifactId>redmine-java-api</artifactId>
            <version>1.23</version>
        </dependency>
    </dependencies>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>2.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>/src/main/assembly/binary.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>cz.ackee.unicorn.PanUnicornMain</mainClass>
                        </manifest>
                        <manifestEntries>
                            <mode>development</mode>
                            <url>${project.url}</url>
                            <key>value</key>
                        </manifestEntries>
                    </archive>
                </configuration>

            </plugin>
        </plugins>
    </build>

</project>

Any idea, why it does compile and run without problem on netbeans and why it does not go well on maven-eclipse project?

任何想法,为什么它在 netbeans 上编译和运行没有问题,为什么它在 maven-eclipse 项目上运行不顺利?

(I compile through command line using mvn package, I run it through eclipse)

(我通过命令行使用编译mvn package,我通过eclipse运行它)

采纳答案by libik

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());

回答by Realdo Dias

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

如果您使用的是简单的HTTP协议(不是HTTPS),而不是:

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

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

Use this:

用这个:

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

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

Just drop the "s" in "HttpsURLConnection"

只需在“Http sURLConnection”中删除“ s

回答by Sachin Panchal

It called if url starts with "https"

如果 url 以“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.

使用netty-3.9.2 > .jar 解决这个问题当你使用这个netty.jar。post call的过程发生了变化。

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

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

回答by Nico

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

为了避免这种错误,您可以对 http 和 https url 使用 HttpUrlConnection (java.net.HttpUrlConnection)

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

HttpsUrlConnection 只为 https 连接添加“可以使用”的额外方法,但您可能不会使用这些方法。

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.

HttpsUrlConnection 和 HttpUrlConnection 派生自 UrlConnection,其中包含允许读取响应的方法,如 getInputStream()。HttpsUrlConnection 派生自 HttpUrlConnection 派生自 UrlConnection。

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

java.net.HttpUrlConnection 只是添加了相对于 http 协议的方法(这些方法适用于 https)

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

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()));