在java

时间:2017-12-26 06:52:43

标签: java proxy

我要求通过代理传递某些特定URL的HTTPs调用并直接休息。我使用java.net的ProxySelector编写了自己的自定义代理实现。它适用于HTTP调用(在这种情况下我可以在代理访问日志中看到)但是在HTTPS调用的情况下,它似乎没有使用代理)。我在这里遗漏了一些东西。代理服务器配置正确,当某些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;
    }
}

1 个答案:

答案 0 :(得分:0)

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