HTTPS Java

    public static String sendHttpsUrlRequestPost(String url, String content, String chatset)
    {
        long start = System.currentTimeMillis();
        String ret = null;
        HostnameVerifier hv = new HostnameVerifier()
        {

            @Override
            public boolean verify(String hostname, SSLSession session)
            {
                return hostname.equals(session.getPeerHost());
            }
        };
        if (StringUtils.isBlank(chatset))
        {
            chatset = CommonConstant.ENCODING_UTF8;
        }
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        HttpURLConnection urlConnect = null;
        try
        {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]
            { new X509TrustManager()
            {

                @Override
                public X509Certificate[] getAcceptedIssuers()
                {
                    return null;
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                {
                    // System.out.println("checkServerTrusted");
                }

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                {
                    // System.out.println("checkClientTrusted");
                }
            } };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("TLS", "SunJSSE");
            sc.getProvider();
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(hv);

            // Now you can access an https URL without having the certificate in
            // the truststore

            URL urlClass = new URL(url);
            urlConnect = (HttpURLConnection) urlClass.openConnection();
            urlConnect.setRequestMethod("POST");
            urlConnect.setConnectTimeout(new Integer(IniConstant.getIniValueByName(
                    IniKey.LOGIN_THIRDPARTY_ALIPAYAPI_TIMEOUT.getKey(), "30000")));
            urlConnect.setReadTimeout(new Integer(IniConstant.getIniValueByName(
                    IniKey.LOGIN_THIRDPARTY_ALIPAYAPI_TIMEOUT.getKey(), "30000")));
            urlConnect.setDoOutput(true);

            urlConnect.setRequestProperty("Content-Length", Integer.toString(content.length())); // 设置请求数据长度

            byte[] byteContent = content.getBytes(chatset);
            urlConnect.getOutputStream().write(byteContent, 0, byteContent.length);
            urlConnect.getOutputStream().flush();
            urlConnect.getOutputStream().close();

            InputStream in = urlConnect.getInputStream();
            ret = getReturnValueFromInputStream(in, chatset);
            in.close();
        } catch (Exception e)
        {
            log.info("请求支付宝异常,请求地址url=" + url + ",请求参数content=" + content);
            throw new BusinessException(e); // http通信异常应直接抛出
        } finally
        {
            log.debug("请求所用时间为" + ((System.currentTimeMillis() - start) / 1000.0) + "s");
            if (urlConnect != null)
            {
                urlConnect.disconnect();
            }
        }
        return ret;
    }

    /**
     * 从输入流获取所有数据并以指定字符集拼装成字符串
     *
     * @param is
     * @param charSet
     * @return
     */
    public static String getReturnValueFromInputStream(InputStream is, String charSet)
    {
        if (StringUtils.isBlank(charSet))
        {
            charSet = CommonConstant.ENCODING_UTF8;
        }

        StringBuilder sb = new StringBuilder();
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(is, charSet));
            char[] temp = new char[1024];
            int charCount = 0;
            while ((charCount = br.read(temp)) != -1)
            {
                sb.append(new String(temp, 0, charCount));
            }
            return sb.toString();
        } catch (Exception e)
        {
            log.fatal(e.getMessage(), e);
            return null;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值