KEEP ON CODING......

HTTP/HTTPS GET&POST两种方式的实现方法

关于GET及POST方式的区别请参照前面文章:
http://www.cnblogs.com/hunterCecil/p/5698604.html

http://www.cnblogs.com/hunterCecil/p/5661459.html

本文具体说明HTTP/HTTPS下GET&POST两种方式的实现方法

具体实现类如下:

public class HttpCommonUtil {

    private HttpCommonUtil () {
    }

    public static String post(String url, String body) {
        if (url.startsWith("https"))
            return httpsPost(url, body);
        else
            return httpPost(url, body);
    }

    private static String httpPost(String url, String body) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(url);
        Response response = target.request()
                .buildPost(Entity.entity(body, MediaType.APPLICATION_JSON)).invoke();
        String re = response.readEntity(String.class);
        response.close();
        LOG.info("http post返回数据: " + re);
        return re;
    }

    private static String httpsPost(String url, String body) {
        ClientConfig clientConfig = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider());
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new ParkingTrustManager()}, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            LOG.error(e);
        } catch (KeyManagementException e) {
            LOG.error(e);
        }
        HostnameVerifier verifier = new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };
        Client client = ClientBuilder.newBuilder().withConfig(clientConfig)
                .sslContext(sc).hostnameVerifier(verifier).build();

        WebTarget target = client.target(url);
        Response response = target.request()
                .buildPost(Entity.entity(body, MediaType.APPLICATION_JSON)).invoke();
        String re = response.readEntity(String.class);
        response.close();
        LOG.info("https post返回数据: " + re);
        return re;
    }

    static class ParkingTrustManager implements TrustManager, X509TrustManager {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

        public void checkClientTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
    }

    public static String get(String url) {
        if (url.startsWith("https"))
            return httpsGet(url);
        else
            return httpGet(url);
    }

    private static String httpsGet(String url) {
        ClientConfig clientConfig = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider());
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new ParkingTrustManager()}, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            LOG.error(e);
        } catch (KeyManagementException e) {
            LOG.error(e);
        }
        HostnameVerifier verifier = new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };

        Client client = ClientBuilder.newBuilder().withConfig(clientConfig)
                .sslContext(sc).hostnameVerifier(verifier).build();

        WebTarget target = client.target(url);
        Response response = target.request().buildGet().invoke();
        String re = response.readEntity(String.class);
        response.close();
        LOG.info("https post返回数据: " + re);
        return re;
    }

    private static String httpGet(String url) {
        Client client = ClientBuilder.newClient();
        WebTarget target = null;
        target = client.target(url);
        Response response = target.request().buildGet().invoke();
        String re = response.readEntity(String.class);
        response.close();
        LOG.info("http post返回数据: " + re);
        return re;
    }
} 
posted @ 2018-01-17 13:46  Cecil2020  阅读(2873)  评论(0编辑  收藏  举报