weblogic.net.http.SOAPHttpsURLConnection 无法转换为 javax.net.ssl.HttpsURLConnection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18512009/
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
weblogic.net.http.SOAPHttpsURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection
提问by KmrGtm
I am getting "java.lang.ClassCastException" while trying to connect to a url using javax.net.ssl.HttpsURLConnection .
我在尝试使用 javax.net.ssl.HttpsURLConnection 连接到 url 时收到“java.lang.ClassCastException”。
I am using Weblogic Server 10.3.4.
我正在使用 Weblogic Server 10.3.4。
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author kmrgtm
*
*/
public class GatewayConnect {
public void ConnectURL()
{
try
{
System.out.println("***** Inside Class File *****");
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
String urlstr="https://www.google.co.in";
URL url = new URL(urlstr);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn = (HttpsURLConnection)url.openConnection();
if (conn instanceof javax.net.ssl.HttpsURLConnection) {
System.out.println("*** openConnection returns an instanceof javax.net.ssl.HttpsURLConnection");
}
if (conn instanceof HttpURLConnection) {
System.out.println("*** openConnection returns an instnace of HttpURLConnection");
}
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader( conn.getInputStream()));
for (String line; (line = reader.readLine()) != null;) {
System.out.println("##### line iz :::"+line);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The exception which i am getting is :
我得到的例外是:
**java.lang.ClassCastException: weblogic.net.http.SOAPHttpsURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection
Inside the MessageSendingAction
***** Inside Class File *****
at com.secureConnect.GatewayConnect.ConnectURL(GatewayConnect.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(Unknown Source)
at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:78)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
at javax.faces.component.UICommand.broadcast(UICommand.java:127)
at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)
at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:103)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:183)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:176)**
Any possible reason of getting this error ?
出现此错误的任何可能原因?
采纳答案by KmrGtm
I got the solution finally. If we are using Weblogic server, we must define:
我终于得到了解决方案。如果我们使用 Weblogic 服务器,我们必须定义:
set JAVA_OPTIONS=%JAVA_OPTIONS% -DUseSunHttpHandler=true
set JAVA_OPTIONS=%JAVA_OPTIONS% -DUseSunHttpHandler=true
...in the class path inside the Server Domain.
...在服务器域内的类路径中。
This will tell the weblogic server to use the Sun Http Handlers and not install its own.
这将告诉 weblogic 服务器使用 Sun Http 处理程序而不是安装它自己的。
回答by Accollativo
More detail on @KmrGtm solution:
有关@KmrGtm 解决方案的更多详细信息:
Only thing to do is updating WebLogic domain settings in the setDomainEnv script file.
唯一要做的就是更新 setDomainEnv 脚本文件中的 WebLogic 域设置。
The relevant settings are in the setDomainEnv script file (setDomainEnv.cmd for Windows and setDomainEnv.sh for Linux).
相关设置位于 setDomainEnv 脚本文件(Windows 的 setDomainEnv.cmd 和 Linux 的 setDomainEnv.sh)中。
The file is located in the bin subdirectory of the domain directory (/user_projects/domains//bin/) where:
该文件位于域目录 (/user_projects/domains//bin/) 的 bin 子目录中,其中:
- is the directory where you installed WebLogic.
- is the name of the domain where you are installing Studio.
- 是您安装 WebLogic 的目录。
- 是您安装 Studio 的域的名称。
In the file add the JAVA_OPTIONS argument close to the top of the file:
在文件中靠近文件顶部的位置添加 JAVA_OPTIONS 参数:
- For setDomainEnv.cmd (Windows):
- 对于 setDomainEnv.cmd (Windows):
set JAVA_OPTIONS=%JAVA_OPTIONS% -DUseSunHttpHandler=true
设置 JAVA_OPTIONS=%JAVA_OPTIONS% -DUseSunHttpHandler=true
- For setDomainEnv.sh (Linux):
- 对于 setDomainEnv.sh (Linux):
JAVA_OPTIONS="%JAVA_OPTIONS% -DUseSunHttpHandler=true"
JAVA_OPTIONS="%JAVA_OPTIONS% -DUseSunHttpHandler=true"
export JAVA_OPTIONS
导出 JAVA_OPTIONS
回答by Naeel Maqsudov
You also may explicitly define protocol handler. Just create URL using another constructor.
您还可以明确定义协议处理程序。只需使用另一个构造函数创建 URL。
URL url = new URL(null, urlstr, new sun.net.www.protocol.http.Handler());
or
或者
URL url = new URL(null, urlstr, new sun.net.www.protocol.https.Handler());
And it will make the code independent from -DUseSunHttpHandler
option.
它将使代码独立于-DUseSunHttpHandler
选项。