Opening HTTPS Connection with Java applet :(

Discussion in 'Java' started by S³awo - MIR, May 4, 2005.

  1. S³awo - MIR

    S³awo - MIR Guest

    Hi :/

    I have a problem like this:

    I try to open HTTPS connection to URL address. Also, I want to send my
    certificate to the server during SSL Hanshake Protocol. Hmmm... and I don't
    know how can I do this <cry>

    Below I put mu code:

    keyStore - is a certificate in object KeyStore



    public String polaczHTTPS(String adres, KeyStore keyStore) {
    try {
    URL adresURL = new URL(adres);
    String odpowiedzSerwera;
    String tekst = "";
    String linia;

    URLConnection polaczenieURL = adresURL.openConnection();

    if (!(polaczenieURL instanceof HttpsURLConnection)){
    return "Adres: " + adres + " nie jest obs³ugiwany przez
    HTTP/HTTPS !!!" ;
    }
    else{
    javax.net.ssl.HttpsURLConnection polaczenie =
    (HttpsURLConnection) polaczenieURL;

    SSLContext kontekstSSL;
    kontekstSSL = SSLContext.getInstance("SSLv3");
    TrustManagerFactory zaufanyMen =
    TrustManagerFactory.getInstance("SunX509");
    zaufanyMen.init(keyStore);
    kontekstSSL.init(null, zaufanyMen.getTrustManagers(), null);
    SSLSocketFactory gniazdoSSL =
    kontekstSSL.getSocketFactory();
    polaczenie.setDefaultSSLSocketFactory(gniazdoSSL);

    polaczenie.connect();

    BufferedReader in = new BufferedReader(new
    InputStreamReader(polaczenie.getInputStream()));

    odpowiedzSerwera = "\n\nAdres: " +
    polaczenie.getURL().toString() +
    "\n\nKodowanie: " +
    polaczenie.getContentEncoding() +
    "\n\nD³ugo¶æ: " +
    polaczenie.getContentLength() +
    "\n\nTyp: " + polaczenie.getContentType();

    while ((linia = in.readLine()) != null) {
    tekst = tekst + linia;
    }
    in.close();

    //polaczenie.disconnect();

    tekst = tekst + "\n\n" + odpowiedzSerwera;
    return tekst;
    }


    }
    catch (IOException e){
    return ("(Klasa PolaczenieHTTPS) Wyj±tek podczas po³±czenia z: "
    + adres + "\n\n" + e.toString());
    }
    catch (KeyStoreException e)
    {
    return ("(Klasa PolaczenieHTTPS) Wyj±tek podczas po³±czenia z: "
    + adres + "\n\n" + e.toString());
    }
    catch (NoSuchAlgorithmException e)
    {
    return ("(Klasa PolaczenieHTTPS) Wyj±tek podczas po³±czenia z: "
    + adres + "\n\n" + e.toString());
    }
    catch (KeyManagementException e)
    {
    return ("(Klasa PolaczenieHTTPS) Wyj±tek podczas po³±czenia z: "
    + adres + "\n\n" + e.toString());
    }
    }
     
    S³awo - MIR, May 4, 2005
    #1
    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. RichRich
    Replies:
    0
    Views:
    793
    RichRich
    Nov 24, 2003
  2. Krista
    Replies:
    3
    Views:
    2,359
    Andrew Thompson
    Sep 15, 2004
  3. Axel
    Replies:
    8
    Views:
    1,268
    Adrienne Boswell
    Apr 27, 2009
  4. jotto
    Replies:
    4
    Views:
    442
    jotto
    Oct 2, 2006
  5. Naveen Dhanuka
    Replies:
    1
    Views:
    319
Loading...

Share This Page