I have done Posting request to https URL(using ssl)in c sharp very easily.
Now I am trying to do the same in Java. I already installed JSSE successfully. Next thing i have to do is to set certificate (i already have valid certificate).
Please, can anyone tell me how to set my certificate?
But thing is that i want to indicate in code my own certificate. I did it in c# as follows:
...
X509Certificate2 certificate = new X509Certificate2(strCertFile, strCertPwd);
...
//Add Client Certificate
WebReq.ClientCertificates.Add(certificate);
...
It is not the whole code, just wanted to show how I add my custom certificate (strCertFile), which i have in my project. I want to do same in Java, and could not find out how to do that?
But thing is that i want to indicate in code my own certificate. I did it in c# as follows:
...
X509Certificate2 certificate = new X509Certificate2(strCertFile, strCertPwd);
...
//Add Client Certificate
WebReq.ClientCertificates.Add(certificate);
...
It is not the whole code, just wanted to show how I add my custom certificate (strCertFile), which i have in my project. I want to do same in Java, and could not find out how to do that?
Are you having difficulty because the server requires a signed client certificate?
But I am writing console application not the web application, therefore i dont need to Set up Tomcat to Require Client Authentication. Can you have a look to my code in C#? i just want the same in java
Code:
//Post Request with custom Certificate
private string PostSSL(string strPage, string strBuffer)
{
try
{
//Our postvars
byte[] buffer = Encoding.UTF8.GetBytes(strBuffer);
//Initialisation
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(strPage);
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
WebReq.KeepAlive = false;
//Import Client Certificate
string strCertFile = Application.StartupPath +
CConsts.csFolder_Certificate + global::E_Terminal.Properties.appl.Default.ClientCertFile;
string strCertPwd = global::E_Terminal.Properties.appl.Default.ClientCertPass;
X509Certificate2 certificate = new X509Certificate2(strCertFile, strCertPwd);
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//Add Client Certificate
WebReq.ClientCertificates.Add(certificate);
WebReq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
//Console.WriteLine(_Answer.ReadToEnd());
return _Answer.ReadToEnd();
}
catch (Exception ex)
{
CUtils.ErrorHandler(ex);
return String.Empty;
}
}
But I am writing console application not the web application, therefore i dont need to Set up Tomcat to Require Client Authentication. Can you have a look to my code in C#? i just want the same in java
Code:
//Post Request with custom Certificate
private string PostSSL(string strPage, string strBuffer)
{
try
{
//Our postvars
byte[] buffer = Encoding.UTF8.GetBytes(strBuffer);
//Initialisation
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(strPage);
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
WebReq.KeepAlive = false;
//Import Client Certificate
string strCertFile = Application.StartupPath +
CConsts.csFolder_Certificate + global::E_Terminal.Properties.appl.Default.ClientCertFile;
string strCertPwd = global::E_Terminal.Properties.appl.Default.ClientCertPass;
X509Certificate2 certificate = new X509Certificate2(strCertFile, strCertPwd);
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//Add Client Certificate
WebReq.ClientCertificates.Add(certificate);
WebReq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
//Console.WriteLine(_Answer.ReadToEnd());
return _Answer.ReadToEnd();
}
catch (Exception ex)
{
CUtils.ErrorHandler(ex);
return String.Empty;
}
}
The top part of the link I posted has configuration for server side. The bottom portion had to do with using PKI with Client apps.
Thanks again, i followed all steps, then even i used Portecle- which allowed me to install trusted certificate. And all went good, but code still give me error:
Thanks again, i followed all steps, then even i used Portecle- which allowed me to install trusted certificate. And all went good, but code still give me error:
Bookmarks