代码
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 javax.servlet.http.HttpServletRequest;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
public static HttpsURLConnection httpsCon(String httpsURL, String query){
try{
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.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);
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setDoInput(true);
return con;
}catch(Exception ce){
log.info("IndoUtil.httpscon()"+IndoUtil.getFullLog(ce));
}
return null;
}
当我将https网址传递给该方法时,我收到以下异常,我无法找到问题,有人请指出
导致异常的实际行是 con.getOutputStream()
HttpsURLConnection con = IndoUtil.httpsCon(httpsURL, query);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
在java.net.SocketInputStream.read(SocketInputStream.java:196)处 java.net.SocketInputStream.read(SocketInputStream.java:122)at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)at at sun.security.ssl.InputRecord.read(InputRecord.java:480)at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:946)at at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1344) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355) 在 sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) 在 sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) 在 sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1093) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250) 在 com.ibm.indo.serviceImpl.GenericServiceImpl.checkDompetkuReg(GenericServiceImpl.java:2042) 在 com.ibm.indo.controller.IndoServiceController.customerProfile(IndoServiceController.java:140) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) 在 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:754)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:847)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) 在com.ibm.indo.util.SessionFilter.doFilter(SessionFilter.java:62) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) 在 org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) 在 org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:671) 在 org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:930) 在java.lang.Thread.run(Thread.java:745)