我在我的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();
}
}
然而,我在我的maven驱动项目的eclipse中使用它,它抛出异常:
java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl
cannot be cast to javax.net.ssl.HttpsURLConnection
这是我的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
}
}
这是我的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>
任何想法,为什么它在netbeans上编译运行没有问题以及为什么它在maven-eclipse项目上不顺利?
(我使用mvn package
通过命令行编译,我通过eclipse运行它)
答案 0 :(得分:21)
解决方案是更改此行
URL url = new URL("https://redmine.xxx.cz/time_entries.xml");
进入这一行
URL url = new URL(null, "https://redmine.xxx.cz/time_entries.xml", new sun.net.www.protocol.https.Handler());
答案 1 :(得分:9)
如果您只使用HTTPS
协议(不是{{1}}),而不是:
HttpsURLConnection httpCon =(HttpsURLConnection) url.openConnection();
使用此:
HttpURLConnection httpCon =(HttpURLConnection)url.openConnection();
只需放弃&#34; s &#34;在&#34; Http s URLConnection&#34;
请注意,此解决方案仅适用于HTTP协议(非HTTPS)
答案 2 :(得分:1)
如果网址以&#34; https&#34;
开头,则会调用 if(url.startswith("https"){ HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection(); }
使用netty-3.9.2&gt; .jar来解决这个问题 当你使用这个netty.jar。通话后的过程发生了变化。
http://dl.bintray.com/netty/downloads/netty-3.9.12.Final.tar.bz2
答案 3 :(得分:1)
为避免此类错误,您可以对http和https url都使用HttpUrlConnection(java.net.HttpUrlConnection)
HttpsUrlConnection仅添加了可以用于https连接的其他方法,但您可能不会使用这些方法。
从UrlConnection派生的HttpsUrlConnection和HttpUrlConnection都包含允许读取响应的方法,例如getInputStream()。从HttpUrlConnection派生的HttpsUrlConnection,而HttpUrlConnection派生自UrlConnection。
java.net.HttpUrlConnection仅添加相对于http协议的方法(这些方法适用于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
因此,您可以这样做:
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
同样,您可以执行此操作并获得相同的结果,连接将相同:
URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));