且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法通过代理建立隧道.代理返回"HTTP/1.1 407".通过https

更新时间:2022-10-31 15:53:19

我发现,当尝试使用https url传递代理时,我们首先发送CONNECT,然后才尝试发送请求.代理服务器无法读取我们附加到请求的任何标题,因为它没有解密流量的密钥. 这意味着CONNECT应该已经具有用户/通向代理的权限才能通过此阶段. 这是我使用的代码快照-适用于我:

What I found out is that when trying to pass through a proxy using https url we first send CONNECT and only then try to send the request. The proxy server cannot read any headrs we attach to the request, cause it doesn't have the key to decrypt the traffic. This means that the CONNECT should already have the user/pass to the proxy to pass this stage. here is a code snap I used - that works for me:

import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.*;

public class ProxyPass {
    public ProxyPass(String proxyHost, int proxyPort, final String userid, final String password, String url) {

        try {
        /* Create a HttpURLConnection Object and set the properties */
            URL u = new URL(url);
            Proxy proxy =
            new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            HttpURLConnection uc = (HttpURLConnection)u.openConnection(proxy);

            Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestorType().equals(RequestorType.PROXY)) {
            return new PasswordAuthentication(userid, password.toCharArray());
            }
            return super.getPasswordAuthentication();
            }
            });

            uc.connect();

            /* Print the content of the url to the console. */
            showContent(uc);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showContent(HttpURLConnection uc) throws IOException {
        InputStream i = uc.getInputStream();
        char c;
        InputStreamReader isr = new InputStreamReader(i);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }

    public static void main(String[] args) {

        String proxyhost = "proxy host";
        int proxyport = port;
        String proxylogin = "proxy username";
        String proxypass = "proxy password";
        String url = "https://....";
        new ProxyPass(proxyhost, proxyport, proxylogin, proxypass, url);

    }
    }

如果您使用的是wink,就像我一样,则需要在ClientConfig中设置代理,然后将其传递给RestClient之前,请设置默认身份验证器.

if you are using wink - like I do, you need to set the proxy in the ClientConfig and before passing it to the RestClient set the default authenticator.

ClientConfig configuration = new ClientConfig();
    configuration.connectTimeout(timeout);

    BasicAuthenticationSecurityHandler basicAuthProps = new BasicAuthenticationSecurityHandler();
    basicAuthProps.setUserName(user);
    basicAuthProps.setPassword(password);
    configuration.handlers(basicAuthProps);

    if (proxySet()) {
        configuration.proxyHost(proxyHost);
        if ((proxyPort != null) && !proxyPort.equals("")) {
            configuration.proxyPort(Integer.parseInt(proxyPort));
        }
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                if (getRequestorType().equals(RequestorType.PROXY)) {
                    return new PasswordAuthentication(proxyUser), proxyPass.toCharArray());
                }
                return super.getPasswordAuthentication();
            }
        });
    }

    restClient = new RestClient(configuration);
    Resource resource = getResource(loginUrl);

    // Request body is empty
    ClientResponse response = resource.post(null);
    if (response.getStatusCode() != Response.Status.OK.getStatusCode()) {
        throw new RestClientException("Authentication failed for user " + user);
    }