J2SE Proxy Authentication

后端 未结 4 1872
孤独总比滥情好
孤独总比滥情好 2021-01-01 06:12

We use 2 SIMILAR Microsoft ISA Proxy Server 2003 to connect to internet. Each Proxy has different Login style, as below :

Server-1 : nt-domain\\alan Server-2 : alan@

4条回答
  •  误落风尘
    2021-01-01 06:50

    Using apache-commons httpClient (version 3), I have the following code. It isn't tested well (if it is at all), but I think it worked once.. :) This is in case you can modify the programs.. if they are some 3rd party packages, nothing you can do.

    
    String proxyHost = System.getProperty("https.proxyHost");
                    int proxyPort = 0;
                    try {
                        proxyPort = Integer.parseInt(System
                                .getProperty("https.proxyPort"));
                    } catch (Exception ex) {
                        //
                    }

                System.setProperty("java.net.useSystemProxies", "true");
    
                ProxySelector ps = ProxySelector.getDefault();
                List proxyList = ps.select(new URI(targetUrl));
                Proxy proxy = proxyList.get(0);
                if (proxy != null) {
                    InetSocketAddress addr = ((InetSocketAddress) proxy
                            .address());
                    if (addr != null) {
                        proxyHost = addr.getHostName();
                        proxyPort = addr.getPort();
                    }
                }
    
                boolean useProxy = proxyHost != null && proxyHost.length() > 0;
    
                if (useProxy) {
                    httpClient.getHostConfiguration().setProxy(proxyHost,
                            proxyPort);
                }
    

提交回复
热议问题