Use Rest Api, Error Respone Code 421 Requested host does not match any names on certificate

ceapon
Contributor
Contributor

api https://api-m.sandbox.paypal.com/v1/oauth2/token

 

runtime environment: java  1.8, tomact 8.5.43.

 

I use postman and local server request token success. but i deploy the server to test environment, request token api fail, Respone error code 421, message Requested host does not match any names on certificate.

 

 Code :

 

 

 

 

public static String sendRequestByPostFormUrlencoded2(String requestUrl, String postData, Map<String, String> headerMap) throws Exception{
                HttpsURLConnection connection=null;
                String response = null;
                try {
                        logger.info("Request url:"+requestUrl);
                        logger.info("Request data:"+postData);
                        
                        SSLContext sc = SSLContext.getInstance("TLSv1.2");
                        try {
                                sc.init(null, null, null);
                        } catch (KeyManagementException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        
            URL url = new URL(requestUrl);
            connection = (HttpsURLConnection)url.openConnection();
            connection.setSSLSocketFactory(sc.getSocketFactory());
            
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
                        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
            connection.setDoOutput(true);
            connection.setDoInput(true);
                        connection.setConnectTimeout(60000); 
                        connection.setReadTimeout(60000); 
                        
                        if(headerMap != null && headerMap.size()>0){
                                for (String header : headerMap.keySet()) {
                                        connection.setRequestProperty(header, headerMap.get(header));
                                }
                        }
                        
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

            //write parameters
            writer.write(postData);
            writer.flush();

            logger.info(connection.getResponseCode());
            logger.info(connection.getResponseMessage());

            // Get the response
            StringBuffer answer = new StringBuffer();
            BufferedReader reader;
            
            if(connection.getResponseCode() == 200 || connection.getResponseCode() == 201){
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            }
            else{
                    reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            }
            
            String line;
            while ((line = reader.readLine()) != null) {
                answer.append(line);
            }
//            writer.close();
            reader.close();

            //Output the response
            logger.info("Response data:"+answer.toString());
            response = answer.toString();
                        connection.disconnect();
                } catch (MalformedURLException e) {
                        e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return response;
        }

private static String getAccessToken(boolean isSandbox) throws Exception {
                Map<String, String> headerMap = new HashMap<String, String>();
                headerMap.put("Authorization", "Basic "+PropertiesUtil.getProperty(GmsConstant.PAYPAL_SANDBOX_SECRET_KEY));
                String postData = "grant_type=client_credentials";
                String responseTxt = HttpUtil.sendRequestByPostFormUrlencoded2(PropertiesUtil.getProperty(GmsConstant.PAYPAL_SANDBOX_TOKEN_URL),postData,headerMap);
                System.out.println("responseTxt=="+responseTxt);
                logger.info("paypalpreorder getAccessToken responseTxt=="+responseTxt);
                JSONObject resultP = JSONObject.parseObject(responseTxt);
                if(resultP.containsKey("access_token")){
                        return resultP.getString("token_type")+" "+resultP.getString("access_token");
                }
                return null;
        }

 

 

 

 

tomcat ssl:

 

 

 

 

<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               keystoreFile="D:\xz\apache-tomcat-8.5.43\ssl\1582943__fengzl.com.pfx"
               keystoreType="PKCS12"  keystorePass="F1beWsIV"
               clientAuth="false"
                           URIEncoding="iso-8859-1"
               SSLProtocol="TLSv1.2+TLSv1.1"
               ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256"/>

 

 

 

 

server host:  https://xztest.fengzl.com

Login to Me Too
1 REPLY 1

ceapon
Contributor
Contributor

only after restarting the service within minutes request ok

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.