Need help with contacting HTTPS URL

Hi,

I am new bie to Java Security. I do not know where to start off with. Here is the requirement.

I need to contact an HTTPS URL and this URL gives me output which is encypted data.

I have to save this ecnrypted data into a file and I have the KEY to decrypt the data.

I have tried several ways (all listed below) to get it working. But I am not successful. I get

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

Can you guys give me some insight on how to proceed?

Thanks

Mathew

import java.util.*;

import java.text.*;

import java.net.*;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.security.Permission;

import javax.net.ssl.HttpsURLConnection;

import java.security.*;

import java.security.cert.*;

import javax.net.ssl.*;

import java.io.*;

publicclass temp

{

protected BufferedReader messageResponseReader;

privatestaticfinal String CERTIFICATE_TYPE ="SunX509";

privatestaticfinal String KEYSTORE_TYPE ="JKS";

privatestaticfinal String SSL_PROTOCOL ="TLS";

privatestaticfinal String CERTIFICATE_FACTORY_TYPE ="X.509";

publicstaticvoid main(String args[])

{

try

{

try{

temp tmp =new temp();

String url ="https://test.mysite.com/one/perform.jsp?mode=get&check=true";

tmp.sendRequest(url);

}catch(Exception e)

{

e.printStackTrace();

}

}

catch(Exception e){

e.printStackTrace();

}

}

public String sendRequest(String urlString)throws Exception

{

StringBuffer response =null;

BufferedReader messageReader =null;

try

{

String username ="user";

String password ="pwd";

String encoding =new sun.misc.BASE64Encoder().encode("username:password".getBytes());

//java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

//System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

// Needed for validation of the server certificate

//System.setProperty("javax.net.ssl.trustStore","C:\\cert\\key.txt");

// Needed for providing a clint certificate for client authentication

//System.setProperty("javax.net.ssl.keyStore","C:\\cert\\key.txt");

//System.setProperty("javax.net.ssl.keyStorePassword","te5t1ng");

//System.setProperty("ssl.SocketFactory.provider", "com.sun.net.ssl.internal.ssl.Provider");

/*

KeyStore ks;

ks = KeyStore.getInstance("JKS");

CertificateFactory cf = CertificateFactory.getInstance(CERTIFICATE_FACTORY_TYPE);

TrustManagerFactory tmf = TrustManagerFactory.getInstance(CERTIFICATE_TYPE);

KeyManagerFactory kmf = KeyManagerFactory.getInstance(CERTIFICATE_TYPE);

FileInputStream fis = new FileInputStream("C:\\cert\\key.txt");

BufferedInputStream bis = new BufferedInputStream(fis);

Collection c = cf.generateCertificates(fis);

Iterator i = c.iterator();

while (i.hasNext()) {

java.security.cert.Certificate cert = (java.security.cert.Certificate)i.next();

System.out.println(cert);

}

ks.load(null, null);

X509Certificate the_cert = (X509Certificate)cf.generateCertificate(bis);

ks.setCertificateEntry("server_cert",the_cert);

tmf.init(ks);

ks = KeyStore.getInstance(KEYSTORE_TYPE);

ks.load(null, null);

the_cert = (X509Certificate)cf.generateCertificate(new FileInputStream("key.txt"));

ks.setCertificateEntry("client_cert",the_cert);

kmf.init(ks, null);

SSLContext ctx = SSLContext.getInstance(SSL_PROTOCOL);

KeyManager[] km = kmf.getKeyManagers();

TrustManager[] tm = tmf.getTrustManagers();

ctx.init (km, tm, null);

HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

*/

urlString = urlString.replaceAll(" ","%20");

URL url =new URL(urlString);

//HttpsURLConnection urlCon = (HttpsURLConnection)url.openConnection();

HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();

//com.sun.net.ssl.HttpsURLConnection urlCon = (com.sun.net.ssl.HttpsURLConnection)urlCon;

/*urlCon.setRequestProperty("Host", url.getHost());*/

urlCon.setDoOutput(true);

urlCon.setDoInput(true);

urlCon.setRequestMethod("POST");

urlCon.setUseCaches (false);

urlCon.setAllowUserInteraction(true);

urlCon.setInstanceFollowRedirects(true);

urlCon.setRequestProperty ("Authorization","Basic " + encoding);

//Permission permision = urlCon.getPermission();

//System.out.println("permission name:"+permision.getName());

urlCon.connect();

//messageReader = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));

//response = new StringBuffer();

//String line;

//while((line = messageReader.readLine()) != null){

//response.append(line);

//response.append("\n");

//}

}

catch (Exception e){

e.printStackTrace();

throw e;

}

return"testing";

}

}

[8518 byte] By [mathewronda] at [2007-10-2 0:39:34]
# 1
Any one please?
mathewronda at 2007-7-15 16:54:24 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 2
Hello,I am getting the same problem(javax.net.ssl.SSLHandshakeException: unknown certificate) when trying to access the HTTPS url with HTTPsURLConnection object.Any help would be appreciated.Thanks,Bala
bala_g6a at 2007-7-15 16:54:24 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 3
i also have problem with open https any one have code plz help me
Sahua at 2007-7-15 16:54:24 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 4
Hi I am also having the same problem. Please let me know if you any one resolved the problemThanks in advance
java06_foruma at 2007-7-15 16:54:24 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 5
Hello!I am struggling with the same problem too...somebody.... :,( anybody?
yamini@suna at 2007-7-15 16:54:24 > top of Java-index,Security,Other Security APIs, Tools, and Issues...