Connecting Via HTTPS

Discussion in 'Java' started by HugeBob, Jul 31, 2008.

  1. HugeBob

    HugeBob Guest

    Hi All,

    I'm trying to import the public key of a secure site (https). But, I'm
    having a lot of difficulty getting this to work. The JRE is on a Unix
    box (SunOS 5.10). From my Windows XP workstation, I went to the site
    with IE and retrieved the public key in DER Binary Format and saved it
    to a *.cer file on the Unix box (via Samba share). I then used the
    keytool to import it into the keystore at /{jrun_root}/jre/lib/
    security/cacerts. I verified that it was in there with the keytool -
    list -keystore cacerts command. Here's the code I'm trying to run.
    But, I get a java.net.ssl.SSLHandshakeException. Can anyone lend some
    guidance here?

    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.net.ssl.*;


    public class WebService {

    public static String sessionCookie;

    public static void main(String [] args) {
    try {
    System.setProperty("javax.net.ssl.keyStore",
    "path_to_keystore");
    System.setProperty("javax.net.ssl.keyStorePassword",
    "keystorepassword");
    System.setProperty("javax.net.ssl.keyStoreType", "JKS");
    System.setProperty("javax.net.ssl.trustStore",
    "path_to_trustkeystore");
    System.setProperty("javax.net.ssl.trustStorePassword",
    "trustStorePassword");

    URL url = new URL("https://somesecureservice.somewhere.com/
    resource");
    HttpsURLConnection conn = (HttpsURLConnection)
    url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Accept", "text,xml");
    conn.setRequestProperty("Content-Type", "text,xml");

    if (sessionCookie != null) {
    conn.setRequestProperty("Cookie", sessionCookie);
    }

    OutputStreamWriter wr = new
    OutputStreamWriter(conn.getOutputStream());
    wr.write("<xmltag xmlns:cr=\"http://someservice.com/something\">");
    wr.write("<xmltaga>somevalue</xmltaga>");
    wr.write("</xmltag");
    wr.flush();

    BufferedReader rd = new BufferedReader(new
    InputStreamReader(conn.getInputStream()));
    System.out.println(conn.getResponseCode());

    String line;
    while ((line = rd.readLine()) != null) {
    System.out.println(line);
    }
    wr.close();
    rd.close();
    }
    catch (Exception t) {
    t.printStackTrace();
    }
    }
    }
    HugeBob, Jul 31, 2008
    #1
    1. Advertising

  2. HugeBob

    Guest

    On Jul 31, 2:12 pm, HugeBob <> wrote:
    > Hi All,
    >
    > I'm trying to import the public key of a secure site (https). But, I'm
    > having a lot of difficulty getting this to work. The JRE is on a Unix
    > box (SunOS 5.10). From my Windows XP workstation, I went to the site
    > with IE and retrieved the public key in DER Binary Format and saved it
    > to a *.cer file on the Unix box (via Samba share). I then used the
    > keytool to import it into the keystore at /{jrun_root}/jre/lib/
    > security/cacerts. I verified that it was in there with the keytool -
    > list -keystore cacerts command. Here's the code I'm trying to run.
    > But, I get a java.net.ssl.SSLHandshakeException. Can anyone lend some
    > guidance here?
    >
    > import java.net.*;
    > import java.io.*;
    > import java.util.*;
    > import java.net.ssl.*;
    >
    > public class WebService {
    >
    > public static String sessionCookie;
    >
    > public static void main(String [] args) {
    > try {
    > System.setProperty("javax.net.ssl.keyStore",
    > "path_to_keystore");
    > System.setProperty("javax.net.ssl.keyStorePassword",
    > "keystorepassword");
    > System.setProperty("javax.net.ssl.keyStoreType", "JKS");
    > System.setProperty("javax.net.ssl.trustStore",
    > "path_to_trustkeystore");
    > System.setProperty("javax.net.ssl.trustStorePassword",
    > "trustStorePassword");
    >
    > URL url = new URL("https://somesecureservice.somewhere.com/
    > resource");
    > HttpsURLConnection conn = (HttpsURLConnection)
    > url.openConnection();
    > conn.setDoOutput(true);
    > conn.setRequestMethod("POST");
    > conn.setRequestProperty("Accept", "text,xml");
    > conn.setRequestProperty("Content-Type", "text,xml");
    >
    > if (sessionCookie != null) {
    > conn.setRequestProperty("Cookie", sessionCookie);
    > }
    >
    > OutputStreamWriter wr = new
    > OutputStreamWriter(conn.getOutputStream());
    > wr.write("<xmltag xmlns:cr=\"http://someservice.com/something\">");
    > wr.write("<xmltaga>somevalue</xmltaga>");
    > wr.write("</xmltag");
    > wr.flush();
    >
    > BufferedReader rd = new BufferedReader(new
    > InputStreamReader(conn.getInputStream()));
    > System.out.println(conn.getResponseCode());
    >
    > String line;
    > while ((line = rd.readLine()) != null) {
    > System.out.println(line);
    > }
    > wr.close();
    > rd.close();
    > }
    > catch (Exception t) {
    > t.printStackTrace();
    > }
    > }
    >
    > }


    You don't need all this stuff with properties.
    If public key is installed on the same JVM (double check it!) then you
    are good to go.

    Alex.
    http://www.myjavaserver.com/~alexfromohio/
    , Jul 31, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adil Hasan
    Replies:
    3
    Views:
    1,102
    John J. Lee
    Jan 29, 2004
  2. harryhans
    Replies:
    0
    Views:
    301
    harryhans
    Oct 20, 2008
  3. Axel
    Replies:
    8
    Views:
    1,060
    Adrienne Boswell
    Apr 27, 2009
  4. jotto
    Replies:
    4
    Views:
    371
    jotto
    Oct 2, 2006
  5. Naveen Dhanuka
    Replies:
    1
    Views:
    261
Loading...

Share This Page