0
点赞
收藏
分享

微信扫一扫

Java如何实现POST(x-www-form-urlencoded)请求

芷兮离离 2024-02-29 阅读 7

public static void main(String[] args) {
//        System.out.println(getDateRange());
        CallAPI();
    }


    private static void CallAPI() {
        try {
            KeyStore ks = KeyStore.getInstance("PKCS12");

            //get certificate file from test/resources as InputFileStream & load to existing keystore
            URL fileURL = new File("C:\\Users\\Admin\\Documents\\API20231221\\1.IND\\2.SLOTS\\PTNEW\\INR\\INR\\INR.p12").toURI().toURL();

            File file = new File(fileURL.getFile());

            FileInputStream fis = new FileInputStream(file);
            ks.load(fis, "fy9OGfZzgA53KxSd".toCharArray());

            //Create KeyManagerFactory using loaded keystore
            KeyManagerFactory kmf =
                    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks, "fy9OGfZzgA53KxSd".toCharArray());
            KeyManager[] kms = kmf.getKeyManagers();

            //Crete TrustManager to bypass trusted certificate check
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }

                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
                        }

                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        }
                    }
            };

            //Hostname verification bypass method
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            //Set connection properties to use bypass certificate/hostname check methods
            SSLContext sslContext = null;
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(kms, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

            //Send API call together with entity key for validation
            HttpsURLConnection connection = (HttpsURLConnection) new URL("https://kioskpublicapi-am.hotspin88.com/player/balance").openConnection();
            connection.setRequestProperty("X_ENTITY_KEY",
                    "e013d5af1d99e349265a27aa4508f0a5ce25b20c2f513d188914d91e6d465147052d61f80bdcb5a9f1ee38b7a080c0e28a294bc1a93ff304dcdb336d8923e2d2");
            connection.setRequestMethod("POST");//设置post
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);
            String data = "playername=9TY_TIM101096";
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(data.getBytes());
            outputStream.flush();
            outputStream.close();
            int responseCode = connection.getResponseCode();//状态
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream response = connection.getInputStream();
                String resp = IOUtils.toString(response);
                System.out.println(resp);
                connection.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

https请求第三方CERT认证

参考地址:https://blog.51cto.com/u_16175497/6912821


举报

相关推荐

0 条评论