0
votes

I need to send request through HTTPS. I have read a lot about SSL and there is lots about SSL on Google but no starting place where to start.

I need to connect to a web service through HTTPS and send and receive data. I am using HttpsUrlConnection to send the requests of. The web service provider does not have a self signed certificate as this would be displayed on the Internet Explorer page. The certificate is issed by COMODA High-Assurance Secure Server CA but is not self signed.

How can I connect, send and receieve data with SSL connections? Please see below code:

public class Sender {

      public void send(String endpoint) {
           URL url = new URL(endpoint);
           HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) url.openConnection();
           httpsUrlConnection.setRequestMethod(new String("POST"));
           httpsUrlConnection.setDoOutput(true);
           httpsUrlConnection.setRequestProperty("Content-Type", "application/xml");
           SSLContext sc = SSLContext.getInstance("SSL");
           sc.init(null, getTrustManager(), new SecureRandom());
           httpsUrlConnection.setSSLSocketFactory(sc.getSocketFactory());
           httpsUrlConnection.connect();
           OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpsUrlConnection.getOutputStream());
           outputStreamWriter.write(""); // writes XML data
           outputStreamWriter.flush();
           outputStreamWriter.close();
           InputStreamReader inputStreamReader = new InputStreamReader(httpsUrlConnection.getInputStream());
           char i = (char) inputStreamReader.read();
           System.out.println(i);
      }

      public TrustManager[] getTrustManager() {
   TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
       public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {

       }

       public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
           System.out.println("AuthType : " + " " + string);
           for(int i = 0; i < xcs.length; i++) {
               System.out.println("\t" + xcs[i].getIssuerX500Principal().getName());
               System.out.println("\t" + xcs[i].getIssuerDN().getName());
           }
       }

       public X509Certificate[] getAcceptedIssuers() {
           return null;
       }
   }};
   return trustAllCerts;

} }

I have read about SSL on: http://wpengine.com/support/how-does-all-this-work-https-ssl-certificates-ca-public-and-private-keys-csrs/ and http://www.instantssl.com/ssl-certificate-products/https.html.

As I mentioned I don't have a self signed certificate but I have used InstallCert.java.

From the above, I receive the following Exception when the httpsUrlConnection.getInputStream() is called

java.io.IOException: Server returned HTTP response code: 500 for URL: // the URL endpoint
java.io.IOException: Server returned HTTP response code: 500 for URL: https://webaddress.co.uk/Service/services/Service?wsdl // not actual address. I can't give this but this is a sample address
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1459)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at com.screening.listener.ConnectionListenerBean.send(ConnectionListenerBean.java:191)
at com.screening.listener.ConnectionListenerBean.processMessage(ConnectionListenerBean.java:129)
at com.screening.listener.ConnectionListenerBean.listen(ConnectionListenerBean.java:107)
at com.screening.listener.ConnectionListenerBean.init(ConnectionListenerBean.java:73)
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.ejb.containers.interceptors.BeanCallbackInterceptor.intercept(InterceptorManager.java:1006)
at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:61)
at com.sun.ejb.containers.interceptors.CallbackInvocationContext.proceed(CallbackInvocationContext.java:109)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCallback(SystemInterceptorProxy.java:133)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.init(SystemInterceptorProxy.java:115)
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.ejb.containers.interceptors.CallbackInterceptor.intercept(InterceptorManager.java:961)
at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:61)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:390)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:373)
at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:518)
at com.sun.ejb.containers.AbstractSingletonContainer.access$100(AbstractSingletonContainer.java:74)
at com.sun.ejb.containers.AbstractSingletonContainer$SingletonContextFactory.create(AbstractSingletonContainer.java:693)
at com.sun.ejb.containers.AbstractSingletonContainer.instantiateSingletonInstance(AbstractSingletonContainer.java:444)
at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:213)
at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:174)
at org.glassfish.ejb.startup.SingletonLifeCycleManager.doStartup(SingletonLifeCycleManager.java:152)
at org.glassfish.ejb.startup.EjbApplication.start(EjbApplication.java:150)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:126)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:241)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:236)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:339)
at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:214)
at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:144)
at org.glassfish.ejb.embedded.EJBContainerImpl.deploy(EJBContainerImpl.java:128)
at org.glassfish.ejb.embedded.EJBContainerProviderImpl.createEJBContainer(EJBContainerProviderImpl.java:120)
at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:102)
at com.screening.Container.createEJBContainer(Container.java:22)
at com.screening.listener.ConnectionListenerBeanTest.setUpClass(ConnectionListenerBeanTest.java:24)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
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 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

What I am doing wrong? Is there anything to add?

From the getTrustManager() four certificates are printed in the Console.

1
can you show the rest of the exception log because 500 does not tell much? - mosaad
Please see edit above. - user3189663
I have 2 questions :Does it work if you connect with HTTP and does HTTPS work if you request from the browser? - mosaad
I don't think it will work with HTTP as the web service provider is an external company. The HTTPS does work from the browser. What is seen is the XML WSDL. - user3189663

1 Answers

1
votes

If you got an HTTP error code of any description, it is proof that the SSL part is working perfectly.

The problem here is the 500, which indicates a server error servicing your request, which has nothing to so with SSL whatsoever. You're going to have to look a the server logs to find out what caused it.