HTTP/HTTPS GET&POST兩種方式的實現方法

關於GET及POST方式的區別請參照前面文章:
http://www.cnblogs.com/hunterCecil/p/5698604.htmlhtml

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

本文具體說明HTTP/HTTPS下GET&POST兩種方式的實現方法dom

公共實現類以下:ide

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;
    }
} 

測試類:post

相關文章
相關標籤/搜索