06-23-2021 01:27 AM
I imported the certificate to java trusted certificates, it shows the some error.is it due to certificate is Self-signed ?.
And is there any way to overcome this problem other than overriding SSL certificate verification.
Is there any way to add SAN to the certificate
I have searched in some website but it shows it is due to the Hostname or common name mismatch but can you tell me how to rectify it
this is the program used:
package A;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("java.home"));
String https_url = "https://10.1.60.60/api/fdm/latest/fdm/token";
String jsonInputString = "{\"grant_type\": \"password\", \"username\": \"admin\", \"password\": \"Ashok@188616\"}";
URL url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
try(OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
print_content(con);
}
private static String print_content(HttpsURLConnection con){
if(con!=null){
try {
// System.out.println("****** Content of the URL ********");
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
StringBuffer res=new StringBuffer();
while ((input = br.readLine()) != null){
res.append(input);
}
System.out.println(res.toString());
String auth;
try {/*
JSONObject myres=new JSONObject(res.toString());
if(myres.has("access_token")) {
auth = myres.getString("access_token");
}else{
auth=null;
}*/
Pattern p1=Pattern.compile("\"access_token\":\"(.*?)\"");
Matcher m1=p1.matcher(res.toString());
m1.find();
auth = m1.group(1);
System.out.println(auth);
}
catch(Exception e){
e.printStackTrace();
return null;
}
//System.out.println(myres.getString("access_token"));
br.close();
return auth;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}else{
return null;
}
}
}
the exception is attached below:-
06-23-2021 03:24 AM
this error happens when I try to use rest Api in java
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide