簡體   English   中英

如何在 java 中使用 .pem 文件證書發布數據

[英]How to post data with .pem file certificate in java

我有板岩集成的要求。 我有一個發布數據的代碼,但我希望它轉換為 java。 以下是供參考的代碼:

'''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();'''

請幫助將 c 代碼轉換為 java 代碼或從 .pem 文件生成證書。 我已經檢查了谷歌中的許多鏈接,但它對我不起作用。 從.pem 文件生成證書時,它會拋出不完整的數據或空數據。

提前致謝,

如果您想閱讀證書,您可以在 java 代碼下方使用此證書。

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

如果你想要別的東西讓我知道

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM