欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java使用https+post

程序员文章站 2022-05-13 16:25:20
...

/*
 * 文 件 名:  GetCSATHttpsTest.java
 * 版    权:  Copyright 2000-2009 Huawei Tech. Co. Ltd. All Rights Reserved.
 * 描    述:  <描述>
 * 修 改 人:  cwx223372
 * 修改时间:  2015年9月11日
 * 修改版本: 
 */
package com.huawei.csservice.action.service;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.junit.Test;

import com.huawei.common.components.encrypt.EncryptedUtils;
import com.huawei.common.components.encrypt.HmacSHA256Encrypter;
import com.huawei.common.system.COMException;
import com.huawei.common.utils.CharsetUtils;
import com.huawei.common.utils.access.jackson.JacksonMapper;
import com.huawei.csservice.action.service.event.GetCSATREvt;

public class GetCSATHttpsTest
{
    public static String httpsurl = "https:www.baidu.com";
   
    /**
     * 测试
     */
    @Test
    public void testHttps()
        throws IOException, NoSuchAlgorithmException, KeyManagementException, COMException
    {
        TrustManager myX509TrustManager = new X509TrustManager()
        {
            @Override
            public X509Certificate[] getAcceptedIssuers()
            {
                return new X509Certificate[] {};
            }
           
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException
            {
            }
           
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException
            {
            }
        };
       
        //设置SSLContext
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] {myX509TrustManager}, null);
       
        // 创建URL对象
        URL myURL = new URL(httpsurl);
       
        // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
        HttpsURLConnection httpsConn = (HttpsURLConnection)myURL.openConnection();
       
        //设置套接工厂
        httpsConn.setSSLSocketFactory(sslcontext.getSocketFactory());
       
        HostnameVerifier hostNameVerify = new HostnameVerifier()
        {
            /**
             * Always return true
             */
            public boolean verify(String urlHostName, SSLSession session)
            {
                return true;
            }
        };
       
        httpsConn.setHostnameVerifier(hostNameVerify);
       
        //加入post数据
        String postdata = getPostData();
        httpsConn.setRequestMethod("POST");
        httpsConn.setDoOutput(true);
        DataOutputStream out = new DataOutputStream(httpsConn.getOutputStream());
        if (null != postdata)
        {
            out.writeBytes(postdata);
        }
        out.flush();
        out.close();
       
        //获取输入流
        BufferedReader in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream()));
        int code = httpsConn.getResponseCode();
       
        if (HttpsURLConnection.HTTP_OK == code)
        {
            String temp = in.readLine();
           
           //輸出字符串
            while (temp != null)
            {
                System.out.println(temp);
                temp = in.readLine();
            }
        }
    }
   
    /**
     * 获取post数据
     */
    private static String getPostData()
        throws UnsupportedEncodingException, COMException
    {
         return "";
    }
}