zoukankan      html  css  js  c++  java
  • 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;
        }
    } 

    测试类:

  • 相关阅读:
    Sprite Kit编程指南(1)-深入Sprite Kit
    无线点菜系统01(需求分析)
    【体系结构】转移预测器性能的定量评价
    数据挖掘的常见方法
    hdu1711
    poj3358数论(欧拉定理)
    爬虫提取非结构化数据
    pyDes 实现 Python 版的 DES 对称加密/解密--转
    python2 'str' object has no attribute 'decode'
    vba传递参数类型错误
  • 原文地址:https://www.cnblogs.com/hunterCecil/p/8000859.html
Copyright © 2011-2022 走看看