• HttpClient代码设置代理


    由于对接faceBook接口,本地测试时候要设置代理才能调试。

    (http和https通用)

    public  SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
            SSLContext sc = SSLContext.getInstance("SSLv3");
    
            // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
            X509TrustManager trustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) throws CertificateException {
                }
    
                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) throws CertificateException {
                }
    
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
    
            sc.init(null, new TrustManager[]{trustManager}, null);
            return sc;
        }
    
        public static HttpClientBuilder proxy(String hostOrIP, int port){
            // 依次是代理地址,代理端口号,协议类型
            HttpHost proxy = new HttpHost(hostOrIP, port, "http");
            DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
            return HttpClients.custom().setRoutePlanner(routePlanner);
        }
    SSLContext sslcontext = null;
    try {
    sslcontext = createIgnoreVerifySSL();
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (KeyManagementException e) {
    e.printStackTrace();
    }
    // 设置协议http和https对应的处理socket链接工厂的对象
           Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
             .register("http", PlainConnectionSocketFactory.INSTANCE)
             .register("https", new SSLConnectionSocketFactory(sslcontext))
             .build();
              PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
              HttpClients.custom().setConnectionManager(connManager);
     //创建自定义的httpclient对象,代理地址和端口
            CloseableHttpClient httpClient = proxy("127.0.0.1", 2222).setConnectionManager(connManager).build();
  • 相关阅读:
    phonegap android 输入法弹出会遮盖表单框的解决办法
    html5 webDatabase 存储中sql语句执行可嵌套使用
    Spring @Cacheable注解无法生效或不起作用的问题整理
    SpringBoot---缓存技术2(基于Redis注解和基于RedisTemplate的手动缓存,包括将缓存数据序列化成json数据,有源码)
    使用document.domain+iframe跨域实例
    为已创建的pgsql设置主键自增
    基于canvas的流程编辑器
    使用RedisTemplate缓存List的一系列操作示例
    kafka
    element-ui table 剧中
  • 原文地址:https://www.cnblogs.com/xiaoheis/p/9300128.html
Copyright © 2020-2023  润新知