title: Using browser's certificate in java program
body: <p>I am trying to make HTTP GET request using HttpURLConnection in java.
  When I make get using browser it says me certificate is not trusted do you want to proceed.
  I accept certificate and GET request get data. but i am getting certificate exception in java( given below )</p>
  
  <p>What i understood from this exception is, I need to download that certificate and put this
  java system property berfore making GET request.</p>
  
  <p>My questions are.</p>
  
  <ol>
  <li>How will download this certificate from browser?</li>
  <li>Can I use browser's certificate store in my java program, what do I need to know to use that?</li>
  <li>If i want to install certificate in my keystore then what do I need to do?</li>
  </ol>
  
  <p>THANKS A LOT :)</p>
  
  <p>I am trying to download certificate using keytool command. I do not have any idea where certificate is stored in server, but i gave the path of server which i use in browser and browser says certificate is not trusted. </p>
  
  <p><img src="https://i.stack.imgur.com/FzvQa.png" alt="enter image description here"></p>
  
  <pre><code>URL gatewayServiceUrl = new URL("http://192.168.55.179:56400/nwa");
          HttpURLConnection connection = (HttpURLConnection) gatewayServiceUrl.openConnection();
          connection.setRequestMethod("GET");
          connection.setRequestProperty("Authorization", getExample.getBasicAuth());
          connection.connect();
          if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
              System.out.println("success");
              System.out.println(getExample.getDataFromStream(connection.getInputStream()));
          } else {
              System.out.println("success");
              System.out.println(getExample.getDataFromStream(connection.getErrorStream()));
          }
          System.out.println(connection.getResponseCode());
  
  
  
  
  
  
  Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.ssl.Alerts.getSSLException(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
      at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
      at sun.security.ssl.Handshaker.processLoop(Unknown Source)
      at sun.security.ssl.Handshaker.process_record(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
      at com.testweb.GetExample.main(GetExample.java:18)
  Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
      at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
      at sun.security.validator.Validator.validate(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
      ... 12 more
  Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
      at java.security.cert.CertPathBuilder.build(Unknown Source)
  </code></pre>