Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Https problem at Win Vista (Unauthorized access).At Windows XP it works.

843790Jul 26 2010
Hi guys,

In my application, I'm accessing xml configuration files via HttpsURLConnection. Lotus Notes server through which I'm accessing them has set the basic authorization.
My code is
public class SSLAuthorize {
    protected String url;
    protected String LoginName;
    protected String LoginPassword;
    protected HttpsURLConnection urlConnection;
    protected String accessMethod;

    public SSLAuthorize(String url, String loginName, String loginPassword, String method) {
        this.url = url;
        this.LoginName = loginName;
        this.LoginPassword = loginPassword;
        this.accessMethod = method;
        this.run();
    }

    private void run(){
        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) {
            }
        } };
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            HostnameVerifier hv = new HostnameVerifier() {
                public boolean verify(String urlHostName, SSLSession session) {
                    return true;
                }
            };
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection
                    .setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            try {
                throw e;
            } catch (Exception e1) {
                e1.printStackTrace();
             }
         }

         HttpsURLConnection con = null;

         if ((con = this.connection()) == null) {
            System.exit(1);
         }

         con.setDoOutput(true);
         try {
             con.setRequestMethod(this.accessMethod);
         } catch (ProtocolException e1) {
             e1.printStackTrace();
         }
         con.setDoInput(true);

         this.authorization(con);
         this.urlConnection = con;
     }

     public HttpsURLConnection getConnection() {
         return this.urlConnection;
     }


    private void authorization(HttpsURLConnection con) {

        String userPassword = this.LoginName + ":" + this.LoginPassword;

        String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
        if (this.accessMethod.equalsIgnoreCase("POST")) {
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        }
        con.setRequestProperty("Authorization", "Basic " + encoding);
    }

   private HttpsURLConnection connection() {
       URL senddata;
       try {
           senddata = new URL(this.url);
           HttpsURLConnection con = null;
           try {
               con = (HttpsURLConnection) senddata.openConnection();
           } catch (IOException e) {
               e.printStackTrace();
           }
           return con;
       } catch (MalformedURLException e) {
           e.printStackTrace();
       }
       return null;
   }
}
This HttpsClient works perfectly at Windows XP. But it fails at the 401 error at Windows Vista and I can't figure out why..:(
The stack is:

java.io.IOException: Server returned HTTP response code: 401 for URL: https://10.96.13.80/circ/incident.nsf/timer-config.xml?readform
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)


Could someone give me a hint what do to do? What could cause this behaviour?
thanks a lot in advance.

Jackxz
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 23 2010
Added on Jul 26 2010
0 comments
206 views