5 Replies Latest reply: Apr 3, 2013 11:50 PM by EJP RSS

    Doubt on sun.net.www.http.HttpClient class whether it supports https protoc

    user637169
      Dear All,

      My requirement is to call HTTPS URL through java code(through HTTP URL call).
      for that I am using sun.net.www.http.HttpClient class.

      I need to know whether sun.net.www.http.HttpClient class supports only HTTP or it supports HTTPS also.

      target host is accessible from the machine where application is running. I checked with telnet command

      I am getting connection refused error when i am accessing HTTPS URL through java code


      TWKOnlineSubmit | submitWithResponse | SocketException : java.net.ConnectException: Connection refused
      java.net.ConnectException: Connection refused
      at sun.reflect.GeneratedConstructorAccessor196.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1491)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1485)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139)
      at com.crimsonlogic.thb.twkonline.TWKOnlineSubmit.submitWithResponse(TWKOnlineSubmit.java:350)
      at com.crimsonlogic.thb.test.servlet.TWKOnlineSubmitServlet.submit(TWKOnlineSubmitServlet.java:128)
      at com.crimsonlogic.thb.test.servlet.TWKOnlineSubmitServlet.doPost(TWKOnlineSubmitServlet.java:86)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
      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.ServletStubImpl.execute(ServletStubImpl.java:183)
      at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3717)
      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)

      Edited by: user637169 on Apr 3, 2013 5:05 AM
        • 1. Re: Doubt on sun.net.www.http.HttpClient class whether it supports https protoc
          805574
          The connection refused exception you are seeing is typically caused by either a firewall blocking the connection or the destination server is not listening at the port you are trying to access.

          Yes Java supports HTTPS connections, there is a built in class javax.net.ssl.HttpsURLConnection dedicated to that.

          Note: Using the com.sun.* or sun.* classes is generally discouraged because it is not part of the public Java API and is may not work in a different JVM implementation or subsequent release. If HttpsURLConnection does not have the functionality you need, I would recommend looking at the Apache HttpComponents library to see if that meets your requirements.
          • 2. Re: Doubt on sun.net.www.http.HttpClient class whether it supports https protoc
            user637169
            Thanks for reply,

            One last question.

            I want to know whether sun.net.www.http.HttpClient api supports HTTPS

            I am using jdk 1.6 version.
            • 3. Re: Doubt on sun.net.www.http.HttpClient class whether it supports https protoc
              EJP
              Why? You shouldn't be using it directly. There are specific and very loud warnings in the Javadoc about that.
              • 4. Re: Doubt on sun.net.www.http.HttpClient class whether it supports https protoc
                user637169
                Thanks for your reply

                Ours is maintenance project.Code is already developed.in that somebody already coded that sun.net.www.protocol.http.httpurlconnection used for connecting HTTP URL. But recently our requirement changed.We need to connect to HTTPS URL with sun.net.www.protocol.http.httpurlconnection without changing this API.

                Now when we are connecting HTTPS URL, we are getting connection reused error.

                Is it a server problem or code problem.

                If it is code problem I want to know whether sun.net.www.protocol.http.httpurlconnection api supports HTTPS URL call
                • 5. Re: Doubt on sun.net.www.http.HttpClient class whether it supports https protoc
                  EJP
                  Ours is maintenance project.Code is already developed.in that somebody already coded that sun.net.www.protocol.http.httpurlconnection used for connecting HTTP URL.
                  So fix it. Change it to java.net.HttpURLConnection.
                  But recently our requirement changed.We need to connect to HTTPS URL with sun.net.www.protocol.http.httpurlconnection without changing this API.
                  No you don't. You need to use javax.net.ssl.HttpsURLConnection, and you will get one automatically when you use an https: URL and call URL.openConnection(). If you aren't going to call any HttpsURLConnection-specific methods you don't need even need to use different code, just a different URL.
                  Now when we are connecting HTTPS URL, we are getting connection reused error.
                  Fix the code as above first and then tell us what happens, if you still have a problem. Do you perhaps mean +'connection refused'?+ That just means there is nothing listening at the host:port specified in the URL. Nothing to do with code at all apart from the URL.
                  Is it a server problem or code problem.
                  It is a code problem. Someone stuck their nose in where it didn't belong. Fortunately it's easy to fix. You only have to change a classname. No other code changes required.
                  If it is code problem I want to know whether sun.net.www.protocol.http.httpurlconnection api supports HTTPS URL call
                  It does by extension. javax.net.ssl.HttpsURLConnection extends java.net.HttpURLConnection. You should not be using the sun.net.www.protocol.http package at all.

                  And none of this has anything overt to do with sun.net.www.http.HttpClient, although both the classes I mentioned use it internally.