JAVA 에서 HTTPS 를 보 내 는 POST 요청 도구 클래스

package com.common.util;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

@SuppressWarnings("deprecation")
public class HttpsUtils 
{ 
     
    /**
     *  HTTPS    POST  
     * @param reqURL     
     * @param params     
     * @return     
     */ 
    @SuppressWarnings({ "finally", "resource" }) 
    public static String sendSSLPostRequest(String reqURL, Map params)
    { 
        long responseLength = 0;                         //     
        String responseContent = null;                   //     
        HttpClient httpClient = new DefaultHttpClient(); //     httpClient   
        X509TrustManager xtm = new X509TrustManager()
        {   //  TrustManager 
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} 
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} 
            public X509Certificate[] getAcceptedIssuers() { return null; } 
        }; 
        try 
        { 
            //TLS1.0 SSL3.0          ,      TLS SSL    ,          SSLContext 
            SSLContext ctx = SSLContext.getInstance("TLS"); 
             
            //  TrustManager        ,TrustManager   SSL Socket    
            ctx.init(null, new TrustManager[]{xtm}, null); 
             
            //  SSLSocketFactory 
            SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); 
             
            //  SchemeRegistry SSLSocketFactory      HttpClient  
            httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory)); 
             
            HttpPost httpPost = new HttpPost(reqURL);                        //  HttpPost 
            List formParams = new ArrayList(); //  POST        
            if(params != null)
            {
	            for(Map.Entry entry : params.entrySet()){ 
	                formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 
	            } 
            }
            httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8")); 
             
            HttpResponse response = httpClient.execute(httpPost); //  POST   
            HttpEntity entity = response.getEntity();             //       
             
            if (null != entity) { 
                responseLength = entity.getContentLength(); 
                responseContent = EntityUtils.toString(entity, "UTF-8"); 
                EntityUtils.consume(entity); //Consume response content 
            } 
            System.out.println("    : " + httpPost.getURI()); 
            System.out.println("    : " + response.getStatusLine()); 
            System.out.println("    : " + responseLength); 
            System.out.println("    : " + responseContent); 
        } 
        catch (Exception e) 
        { 
            e.printStackTrace(); 
        } 
        finally 
        { 
            httpClient.getConnectionManager().shutdown(); //    ,     
            return responseContent; 
        } 
    } 
    
    @SuppressWarnings({ "finally", "resource" })
	public static String sendSSLPostBodyRequest(String reqURL, String bodyStr)
    { 
        long responseLength = 0;                         //     
        String responseContent = null;                   //     
        HttpClient httpClient = new DefaultHttpClient(); //     httpClient   
        X509TrustManager xtm = new X509TrustManager()
        {   //  TrustManager 
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} 
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} 
            public X509Certificate[] getAcceptedIssuers() { return null; } 
        }; 
        try 
        { 
            //TLS1.0 SSL3.0          ,      TLS SSL    ,          SSLContext 
            SSLContext ctx = SSLContext.getInstance("TLS"); 
             
            //  TrustManager        ,TrustManager   SSL Socket    
            ctx.init(null, new TrustManager[]{xtm}, null); 
             
            //  SSLSocketFactory 
            SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); 
             
            //  SchemeRegistry SSLSocketFactory      HttpClient  
            httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory)); 
             
            HttpPost httpPost = new HttpPost(reqURL);                        //  HttpPost 
            if(!StringUtils.isBlank(bodyStr))
            {
	            StringEntity requestEntity = new StringEntity(bodyStr, "UTF-8");
	            httpPost.setEntity(requestEntity); 
            }
             
            HttpResponse response = httpClient.execute(httpPost); //  POST   
            HttpEntity entity = response.getEntity();             //       
             
            if (null != entity) { 
                responseLength = entity.getContentLength(); 
                responseContent = EntityUtils.toString(entity, "UTF-8"); 
                EntityUtils.consume(entity); //Consume response content 
            } 
            System.out.println("    : " + httpPost.getURI()); 
            System.out.println("    : " + response.getStatusLine()); 
            System.out.println("    : " + responseLength); 
            System.out.println("    : " + responseContent); 
        } 
        catch (Exception e) 
        { 
            e.printStackTrace(); 
        } 
        finally 
        { 
            httpClient.getConnectionManager().shutdown(); //    ,     
            return responseContent; 
        } 
    }

} 

좋은 웹페이지 즐겨찾기