The Artima Developer Community
Sponsored Link

Java Answers Forum
problem with http authentication

4 replies on 1 page. Most recent reply: Aug 28, 2003 8:22 AM by Patrick Boisclair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Stephen Huey

Posts: 10
Nickname: stephen
Registered: Aug, 2003

problem with http authentication Posted: Aug 10, 2003 6:45 PM
Reply to this message Reply
Advertisement
I'm having trouble figuring out how to interact with an HTTPS site from a Java application (not applet or server-side) and submit some forms until I get to the page I need to parse for some data. In order to make sure I could figure out how to do *something*, I followed some O'Reilly Network Programming code for posting to a website (pg. 500-501) and created a Java application that can interact with http://www.weather.com and submit my local zip code and get the HTML from the result page. From there I tried to modify it to interact with the HTTPS site. I wasn't sure what I needed to change, especially with the following line:

URLConnection uc = url.openConnection();

However, I found this code online and decided to put it in after the above line:

if(uc instanceof javax.net.ssl.HttpsURLConnection ) {
System.out.println("very good");
}
else {
System.out.println("ooops, failed");
}

Because the URL I hardcoded is an https one, it prints out "very good", so I know it's automatically giving me an HttpsURLConnection.

I also used Authenticator.setDefault(mySpecificAuthenticatorSubclass());

However, when I run the code to try to interact with the site, I get this:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

So, I'm clearly not authenticating properly or something like that. Any ideas? This is actually about the 3rd approach I've taken in the past week, and I'm getting pretty frustrated!


Patrick Boisclair

Posts: 4
Nickname: pboisclair
Registered: Aug, 2003

Re: problem with http authentication Posted: Aug 27, 2003 12:44 PM
Reply to this message Reply
I have a similar situation to the one describe here. I have been hunting for a solution now for about a week.

The application works perfectly with a Http connection, but does nothing and generates an exception when using Https.

Here are the stack traces:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:
No trusted certificate found
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SunJSSE_az.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SunJSSE_az.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SunJSSE_ax.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.j(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(DashoA6275)
at sun.net.www.protocol.https.HttpsClient.afterConnect(DashoA6275)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(DashoA627 5)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.j ava:558)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getOutput Stream(DashoA6275)
at com.unitrin.ami.test.AmiClient.sendRequest(AmiClient.java:129)

swan

Posts: 7
Nickname: swan
Registered: Aug, 2003

Re: problem with http authentication Posted: Aug 27, 2003 8:16 PM
Reply to this message Reply
Hi,

You might need JSSE (Java Secure Sockets Extension version 1.0.3) api or above to implement the https thing. JSSE is a set of Java packages that enable secure Internet communications.

You can download jsse from http://java.sun.com/products/jsse/index-103.html

You might also need a certificate. You can get a test certificate from either thawte.com or verisign.com

The JSSE documentation can help you in making use of the above.

Hope it helps.

Patrick Boisclair

Posts: 4
Nickname: pboisclair
Registered: Aug, 2003

Re: problem with http authentication Posted: Aug 28, 2003 8:16 AM
Reply to this message Reply
I have followed the indication from another post
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException

The suggestion from David worked for our situation. We are using JAVA SDK 1.4.2 for Windows. This release include JSSE 1.0.3.

I saved the server certificate using IE in the jre\lib\security folder. Then I used keytool to import the certificate information in cacerts.

This is code I used to send/receive the request. I do not know if I need everything in there, but it works and it is something to start with:
public void sendRequest  ( )
{
if ( responseFileName.trim( ).length( ) == 0)
    {
    responseFileName = requestFileName + ".out.xml" ;
    }
 
try
    {
    //    Construct the request connection to service
 
    System.setProperty("java.protocol.handler.pkgs", 
        "com.sun.net.ssl.internal.www.protocol" ) ;
 
    URL
        url = new URL ( serviceUrl );
 
    URLConnection
        urlConnection = (URLConnection) url.openConnection();
 
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
 
    OutputStream
        requestOutputStream = urlConnection.getOutputStream();
 
    writeFileToStream ( requestOutputStream ) ;
 
    requestOutputStream.close();
 
    //    Get ready to read the response
 
    InputStream
        responseInputStream = null;
 
    try
        {
        responseInputStream = urlConnection.getInputStream();
        }
    catch (Exception e)
        {
        displayResponse ( urlConnection ) ;
        return;
        }
 
    displayResponse ( urlConnection ) ;
    writeStreamToFile ( responseInputStream ) ;
    }
catch (Throwable e)
    {
    e.printStackTrace();
    }
}


Let me know if it helped!!

Patrick Boisclair

Posts: 4
Nickname: pboisclair
Registered: Aug, 2003

Re: problem with http authentication Posted: Aug 28, 2003 8:22 AM
Reply to this message Reply
Correction, the suggestion was from Stephen, not David.

Flat View: This topic has 4 replies on 1 page
Topic: setting Focus Previous Topic   Next Topic Topic: ClassCastException

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use