Need to do a GET&POST HTTPS Request using a .cer c

2019-09-14 21:50发布

问题:

I have a .cer self-signed certificate using which I need to make a GET/POST Request to a Webservice in JAVA. I'm totally new to this concept. I have googled a lot about how to do this, but nothing helped me. While doing this, I came across java keystore & truststore & I guess this can be used to accomplish my task. Can someone be kind enough to help me understand how java Keystore works. Please suggest if there is some other way to do this. Any kind of help is appreciated. Thanks in advance.

回答1:

Basically, a keystore is "place" (usually a file) where you can put digital certificates and its corresponding private keys (you'll have the private key only if you're the owner of the certificate - that's a simplified explanation, but I think it's good enough for now)

When you get/post to a https URL, it means that the server you're accessing has its own digital certificate. And to successfully access it, you need to trust that certificate. In java you can do it by creating a keystore that contains the certificate and the corresponding chain (each certificate is signed by some other entity who also has a certificate, or it's self signed. So you get the certificate who signed it, the certificate who signed the signer, and so on, until you get to a self signed one, and all of these certificates are the chain - you need to put all the chain certificates inside the keystore) This specific keystore is called truststore (the store that contains all trusted certificates).

The self signed certificate that you have is the service's certificate? If so, just create a keystore with it and set it as your trust store.

How to create a keystore: https://docs.oracle.com/cd/E19509-01/820-3503/ggfen/index.html (or search examples for the Keystore class)

How to set trust store: java SSL and cert keystore