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

    测试类:

  • 相关阅读:
    常用的VI/VIM命令
    那些年学过的一些算法
    huffman编码
    好用java库(一):java date/time api:jodatime
    linux启动
    ubuntu学习方式
    地址
    各种各样的软件
    jquery文件
    C变量与数据
  • 原文地址:https://www.cnblogs.com/hunterCecil/p/8000859.html
Copyright © 2020-2023  润新知