0

I want to do an online xml request in java but the server responds with 401 error that means that there is an authentication that is need to access the server. I have the certfile.cer that i can use to do the authentication but i dont know how to load it in java.How can I achieve this in java? Here is part of my code.

StringBuilder answer = new StringBuilder();
URL url = new URL("www.myurl.com");
URLConnection conn = url.openConnection();
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(xml);
writer.flush();
 String line;
 while ((line = reader.readLine()) != null)
 {
 answer.append(line);
 }
MorganM
  • 195
  • 1
  • 1
  • 10
  • 1
    possible duplicate of [Java HTTPS client certificate authentication](http://stackoverflow.com/questions/1666052/java-https-client-certificate-authentication) – Jens Jan 15 '15 at 06:12
  • In addition to that SO question linked by @Jens, also check this link: http://vafer.org/blog/20061010073725/ (it contains a Java code example) – nhylated Jan 15 '15 at 06:15
  • @nhylated how can convert the cert file to a keystore? – MorganM Jan 15 '15 at 06:49
  • @MorganM Check the SO question linked by @Jens: `keytool -import -keystore ./client-truststore.jks -file myca.crt -alias myca` – nhylated Jan 15 '15 at 06:59
  • @nhylated I have managed to generate the keystore here is my code that is now giving me null pointer exception. HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setSSLSocketFactory(getFactory(new File("client-truststore.jks"), "mypass"));//here is where the null pointer error is OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); – MorganM Jan 15 '15 at 07:53

0 Answers0