Solved when accessing  an HTTP site.

Posted on 2006-05-19
Last Modified: 2008-02-01
I'm trying to access an HTTP site.
In my method I have this line: Authenticator.setDefault(new MyAuthenticator());

I also have a subclass:
public class MyAuthenticator extends Authenticator {
  protected PasswordAuthentication getPasswordAuthentication() {
    // Get information about the request
    String promptString = getRequestingPrompt();
    String hostname     = getRequestingHost();
    InetAddress ipaddr  = getRequestingSite();
    int port            = getRequestingPort();
    // Return the information
    return new PasswordAuthentication(
    USERNAME, PASSWORD.toCharArray());
} // end class MyAuthenticator

This usually works okay for me.
But when I ran my program for this particular HTTP site, I got this exception: No trusted certificate found.

I'm not sure how to handle this situation. Could somebody please advice?

Thanks in advance for the help.
Question by:rnicholus
    LVL 86

    Accepted Solution

    You can elect to trust that site using custom socket
    LVL 3

    Expert Comment


    Just specify the keystore that contains trusted certificates. See the documentation that came with J2SDK. In case of J2SDK 1.4, you can find it at:


    With your trusted certificate issue, you probably need to  add the certificate of the machine that you're authenticating into to Java's "cacerts" trusted certificate keystore using keytool. Be careful because in Windows you may have  two copies of "cacerts" on your machine: a copy in your original Java installation directory, such as <C:\java\jre\lib\security\cacerts>, and one copy in the Java runtime environment's Program Files directory installation, such as <C:\Program Files\Java\j2re1.4.1_03\lib\security>.

     Java looks to the "cacerts" in the Program Files installation directory for trusted certificates, but to be safe you should add the certificate to both "cacerts" keystores.

    Author Comment


    I tried your suggestion.
    When I called  InputStream tempStream = url.openStream();   ---> url is an URL object. I got an exception: "HTTPS hostname wrong: should be..."

    I check the host name and it's actually okay. I'm not sure why I get that exception.
    LVL 92

    Expert Comment

    Specify a HostnameVerifier:

      HostnameVerifier hv = new HostnameVerifier()
            public boolean verify(String urlHostName, SSLSession session)
                return true;
    LVL 86

    Expert Comment


    Author Comment

    As a note,

    Solution from objects will work to solve the "HTTPS hostname wrong: should be ... "
    But I found same solution somewhere else before objects posted.

    Thank you guys,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 67
    NotAlone Challenge 20 65
    return in catch statement 1 32
    wordlen challenge 3 26
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now