Https 请求工具(put,post,get)

package com.util;
/**
 * @Description: 类描述
 * @author 作者 ll E-mail:80002132@sf-express.com
 * @version 1.0
 * @date 创建时间:2017年3月15日 下午6:13:49
 */
import java.io.ByteArrayOutputStream;  
import java.io.DataOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.net.URL;  
import java.security.KeyManagementException;  
import java.security.NoSuchAlgorithmException;  
import java.security.cert.CertificateException;  
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

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.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConstants;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSON;  
  
public class HttpsUtil {  
  
    private static class TrustAnyTrustManager implements X509TrustManager {  
  
        public void checkClientTrusted(X509Certificate[] chain, String authType)  
                throws CertificateException {  
        }  
  
        public void checkServerTrusted(X509Certificate[] chain, String authType)  
                throws CertificateException {  
        }  
  
        public X509Certificate[] getAcceptedIssuers() {  
            return new X509Certificate[] {};  
        }  
    }  
  
    private static class TrustAnyHostnameVerifier implements HostnameVerifier {  
        public boolean verify(String hostname, SSLSession session) {  
            return true;  
        }  
    }  
  
    /** 
     * post方式请求服务器(https协议) 
     *  
     * @param url 
     *            请求地址 
     * @param content 
     *            参数 
     * @param charset 
     *            编码 
     * @return 
     * @throws NoSuchAlgorithmException 
     * @throws KeyManagementException 
     * @throws IOException 
     */  
    public static String post(String url, String content)  
            throws NoSuchAlgorithmException, KeyManagementException,  
            IOException {  
        SSLContext sc = SSLContext.getInstance("SSL");  
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },  
                new java.security.SecureRandom());  
  
        URL console = new URL(url);  
        HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();  
        conn.setSSLSocketFactory(sc.getSocketFactory());  
        conn.setRequestMethod("POST");
        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());  
        conn.setDoOutput(true);  
        conn.setRequestProperty("Content-Type", "application/json");
        conn.connect();  
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());  
        out.write(content.getBytes("UTF8"));  
        // 刷新、关闭  
        out.flush();  
        out.close();  
        InputStream is = conn.getInputStream();  
        if (is != null) {  
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
            byte[] buffer = new byte[1024];  
            int len = 0;  
            while ((len = is.read(buffer)) != -1) {  
                outStream.write(buffer, 0, len);  
            }  
            is.close();  
            return outStream.toString();  
        }  
        return null;  
    } 
    
    /** 
     * post方式请求服务器(https协议) 
     *  
     * @param url 
     *            请求地址 
     * @param content 
     *            参数 
     * @param charset 
     *            编码 
     * @return 
     * @throws NoSuchAlgorithmException 
     * @throws KeyManagementException 
     * @throws IOException 
     */  
    public static String put(String url, String content)  
            throws NoSuchAlgorithmException, KeyManagementException,  
            IOException {  
        SSLContext sc = SSLContext.getInstance("SSL");  
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },  
                new java.security.SecureRandom());  
  
        URL console = new URL(url);  
        HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();  
        conn.setRequestMethod("PUT");
        conn.setSSLSocketFactory(sc.getSocketFactory());  
        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());  
        conn.setDoOutput(true);  
        conn.setRequestProperty("Content-Type", "application/json");
        conn.connect();  
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());  
        out.write(content.getBytes("UTF8"));  
        // 刷新、关闭  
        out.flush();  
        out.close();  
        InputStream is = conn.getInputStream();  
        if (is != null) {  
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
            byte[] buffer = new byte[1024];  
            int len = 0;  
            while ((len = is.read(buffer)) != -1) {  
                outStream.write(buffer, 0, len);  
            }  
            is.close();  
            return outStream.toString();  
        }  
        return null;  
    } 
    
    public static void main(String [] args) throws KeyManagementException, NoSuchAlgorithmException, IOException{
    	Map<String,Object> map=new HashMap<String,Object>();
    	HttpsUtil.post("url", JSON.toJSONString(map));
    }
    
    

}  

  

posted on 2017-03-17 10:11  左侧码工  阅读(876)  评论(0编辑  收藏  举报