简体   繁体   English

在Java中将代理用于特定的HTTPS请求

[英]Using proxy for specific HTTPS request in java

I have requirement to pass HTTPs calls of some specific URL via proxy and rest direct. 我需要通过代理传递某些特定URL的HTTPs调用,然后直接休息。 I have written my own custom proxy implementation using ProxySelector of java.net. 我已经使用java.net的ProxySelector编写了自己的自定义代理实现。 It is working fine for HTTP calls ( I can see in proxy access logs in that case) but in case of HTTPS calls it seems it is not using proxy).Am I missing something here.? HTTP调用可以正常工作(在这种情况下,我可以在代理访问日志中看到),但在HTTPS调用的情况下,似乎没有使用代理)。我在这里缺少什么吗? Proxy server is configured properly and its access log is updating when some HTTPS calls passed from browser with proxy. 从浏览器通过代理传递一些HTTPS调用时,代理服务器已正确配置,并且其访问日志正在更新。

package com.blabla.proxy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.vuclip.pubsub.logging.PubSubUtil;
import com.vuclip.pubsub.logging.client.GooglePubSubClient;

public class CustomProxySelector extends ProxySelector {

    private static final Logger LOGGER = LogManager.getLogger(PubSubUtil.class);
    private final ProxySelector def;
    private final String PUB_SUB_URL = "pubsub.googleapis.com";
    List<Proxy> proxyList = new ArrayList<Proxy>();
    private Proxy proxy=null;

    public CustomProxySelector(ProxySelector aDefault) {
        this.def = aDefault;
    }

    @Override
    public void connectFailed(URI arg0, SocketAddress soc, IOException ex) {
        LOGGER.error("Error in connecting to proxcy "+soc +" for pubsub :"+ ex);
    }

    @Override
    public List<Proxy> select(URI uri) {

        if ("https".equalsIgnoreCase(uri.getScheme()) && uri.getHost().startsWith(PUB_SUB_URL)
                && GooglePubSubClient.isProxyEnabled()) {

            synchronized (this) {
                if (proxy == null) {
                    proxy = new Proxy(Proxy.Type.SOCKS,
                            new InetSocketAddress(GooglePubSubClient.getProxyHost(), GooglePubSubClient.getProxyPort()));
                }
            }

            proxyList.add(proxy);
            LOGGER.debug("ProxyList:" + proxyList);
            return proxyList;
        }
        proxyList = def.select(uri);
        LOGGER.debug("Default proxy list : " + proxyList);
        return proxyList;
    }
}

我将Proxy.Type.SOCKS更改为Proxy.Type.HTTP,它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM