简体   繁体   中英

How to post data with .pem file certificate in java

I have a requirement for slate integration. I have a code for posting data but I want it to convert into java. Below is the code for reference:

'''string host = @url;
string certName = @"myfile.pfx"; // i am having .pem file
string password = @"password"; // no password
var certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(certName, 
 password);        
System.Net.ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(host);
req.PreAuthenticate = true;
req.Credentials = new System.Net.NetworkCredential("username", "");
req.ClientCertificates.Add(certificate);
req.Method = "POST";
req.ContentType = "text/xml";
string postData = "<hello>world</hello>";
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData);
req.ContentLength = postBytes.Length;
req.GetRequestStream().Write(postBytes, 0, postBytes.Length);
req.GetRequestStream().Close();
var resp = req.GetResponse();'''

Please help in converting c code to java code or in generating a certificate from.pem file. I have checked many links in google but it's not working for me. It is throwing incomplete data or empty data while generating certificate from.pem file.

Thanks in advance,

If you want read the certificate you can use this below java code.

CertificateFactory fact = CertificateFactory.getInstance("X.509"); FileInputStream is = new FileInputStream (pemfilepath); X509Certificate cer = (X509Certificate) fact.generateCertificate(is); PublicKey key = cer.getPublicKey();

If you want something else let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM