Access a servlet via HTTPS using a standalone client
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   solus_andro
Posted On:   Monday, June 27, 2005 01:32 PM

Hi, I want to access a servlet via HTTPS and post some data to it using a standalone client. here is my code : HttpsURLConnection connection = null; URL url = null; File f = new File("c:/test.data"); SSLContext context = null; try { context = SSLContext.getInstance("SSL"); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); KeyStore ks = KeyStore.getInstance("JKS"); TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); char[] password ="password".toCharArray(); ks.load(new FileInputStream("C:/Documents and Set   More>>


Hi,

I want to access a servlet via HTTPS and post some data to it using a standalone client.

here is my code :

			
HttpsURLConnection connection = null;
URL url = null;
File f = new File("c:/test.data");


SSLContext context = null;
try {
context = SSLContext.getInstance("SSL");

KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
TrustManagerFactory tmf =
TrustManagerFactory.getInstance("SunX509");

char[] password ="password".toCharArray();
ks.load(new FileInputStream("C:/Documents and Settings/Lee/.keystore"),password);
tmf.init(ks);
kmf.init(ks,password);
context.init(kmf.getKeyManagers(),tmf.getTrustManagers(),null);

} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
url = new URL("https://localhost/test/servletTest");

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


try {
HttpsURLConnection.setDefaultSSLSocketFactory(fact);


connection = (HttpsURLConnection) url.openConnection();


connection.setSSLSocketFactory(fact);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setDefaultUseCaches(false);
connection.setAllowUserInteraction(false);


OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream());

FileReader fr = new FileReader(f);

BufferedReader ins = new BufferedReader(fr);

String ss = null;
while ((ss = ins.readLine()) != null) {
out.write(ss+"
");
out.flush();
}

out.close();


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

System.out.println(in.readLine());


//connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}


But I keep getting this error message :

			
java.io.IOException: HTTPS hostname wrong: should be localhost
at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing(HttpsClient.java:493)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:418)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:170)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:828)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
at App.main(App.java:144)



Can someone tell me why it doesnt like it when I add the servlet information (/test/servletTest)? What can I do to make it work?

Thanks

Solus
   <<Less
About | Sitemap | Contact