java.net.Proxy.Type Java Examples
The following examples show how to use
java.net.Proxy.Type.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RestUtils.java From huaweicloud-sdk-java-obs with Apache License 2.0 | 6 votes |
public static void initHttpProxy(OkHttpClient.Builder builder, String proxyHostAddress, int proxyPort, final String proxyUser, final String proxyPassword, String proxyDomain, String proxyWorkstation) { if (proxyHostAddress != null && proxyPort != -1) { if (log.isInfoEnabled()) { log.info("Using Proxy: " + proxyHostAddress + ":" + proxyPort); } builder.proxy(new java.net.Proxy(Type.HTTP, new InetSocketAddress(proxyHostAddress, proxyPort))); if (proxyUser != null && !proxyUser.trim().equals("")) { Authenticator proxyAuthenticator = new Authenticator() { @Override public Request authenticate(Route route, Response response) throws IOException { String credential = Credentials.basic(proxyUser, proxyPassword); return response.request().newBuilder().header(CommonHeaders.PROXY_AUTHORIZATION, credential) .build(); } }; builder.proxyAuthenticator(proxyAuthenticator); } } }
Example #2
Source File: AbstractWeixinmpController.java From weixinmp4java with Apache License 2.0 | 6 votes |
/** * 初始化HTTP代理服务器 */ private void initHttpProxy() { // 初始化代理服务器 if (httpProxyHost != null && httpProxyPort != null) { SocketAddress sa = new InetSocketAddress(httpProxyHost, httpProxyPort); proxy = new Proxy(Type.HTTP, sa); // 初始化代理服务器的用户验证 if (httpProxyUsername != null && httpProxyPassword != null) { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(httpProxyUsername, httpProxyPassword.toCharArray()); } }); } } }
Example #3
Source File: HttpClientService.java From cppcheclipse with Apache License 2.0 | 6 votes |
private Proxy getProxyFromProxyData(IProxyData proxyData) throws UnknownHostException { Type proxyType; if (IProxyData.HTTP_PROXY_TYPE.equals(proxyData.getType())) { proxyType = Type.HTTP; } else if (IProxyData.SOCKS_PROXY_TYPE.equals(proxyData.getType())) { proxyType = Type.SOCKS; } else if (IProxyData.HTTPS_PROXY_TYPE.equals(proxyData.getType())) { proxyType = Type.HTTP; } else { throw new IllegalArgumentException("Invalid proxy type " + proxyData.getType()); } InetSocketAddress sockAddr = new InetSocketAddress(InetAddress.getByName(proxyData.getHost()), proxyData.getPort()); Proxy proxy = new Proxy(proxyType, sockAddr); if (!Strings.isNullOrEmpty(proxyData.getUserId())) { Authenticator.setDefault(new ProxyAuthenticator(proxyData.getUserId(), proxyData.getPassword())); } return proxy; }
Example #4
Source File: AbstractWeixinmpController.java From weixinmp4java with Apache License 2.0 | 6 votes |
/** * 初始化HTTP代理服务器 */ private void initHttpProxy() { // 初始化代理服务器 if (httpProxyHost != null && httpProxyPort != null) { SocketAddress sa = new InetSocketAddress(httpProxyHost, httpProxyPort); proxy = new Proxy(Type.HTTP, sa); // 初始化代理服务器的用户验证 if (httpProxyUsername != null && httpProxyPassword != null) { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(httpProxyUsername, httpProxyPassword.toCharArray()); } }); } } }
Example #5
Source File: LoadProxiesListener.java From LambdaAttack with MIT License | 6 votes |
@Override public void actionPerformed(ActionEvent actionEvent) { int returnVal = fileChooser.showOpenDialog(frame); if (returnVal == JFileChooser.APPROVE_OPTION) { Path proxyFile = fileChooser.getSelectedFile().toPath(); LambdaAttack.getLogger().log(Level.INFO, "Opening: {0}.", proxyFile.getFileName()); botManager.getThreadPool().submit(() -> { try { List<Proxy> proxies = Files.lines(proxyFile).distinct().map((line) -> { String host = line.split(":")[0]; int port = Integer.parseInt(line.split(":")[1]); InetSocketAddress address = new InetSocketAddress(host, port); return new Proxy(Type.SOCKS, address); }).collect(Collectors.toList()); LambdaAttack.getLogger().log(Level.INFO, "Loaded {0} proxies", proxies.size()); botManager.setProxies(proxies); } catch (Exception ex) { LambdaAttack.getLogger().log(Level.SEVERE, null, ex); } }); } }
Example #6
Source File: ProxyFactory.java From google-cloud-eclipse with Apache License 2.0 | 6 votes |
@VisibleForTesting public Proxy createProxy(URI uri) { Preconditions.checkNotNull(uri, "uri is null"); Preconditions.checkArgument(!"http".equals(uri.getScheme()), "http is not a supported schema"); IProxyService proxyServiceCopy = proxyService; if (proxyServiceCopy == null) { return Proxy.NO_PROXY; } IProxyData[] proxyDataForUri = proxyServiceCopy.select(uri); for (final IProxyData iProxyData : proxyDataForUri) { switch (iProxyData.getType()) { case IProxyData.HTTPS_PROXY_TYPE: return new Proxy(Type.HTTP, new InetSocketAddress(iProxyData.getHost(), iProxyData.getPort())); case IProxyData.SOCKS_PROXY_TYPE: return new Proxy(Type.SOCKS, new InetSocketAddress(iProxyData.getHost(), iProxyData.getPort())); default: logger.warning("Unsupported proxy type: " + iProxyData.getType()); break; } } return Proxy.NO_PROXY; }
Example #7
Source File: PollingStatusServiceImpl.java From google-cloud-eclipse with Apache License 2.0 | 6 votes |
private Proxy getProxy(URI uri) { if (proxyService == null) { return Proxy.NO_PROXY; } IProxyData[] proxies = proxyService.select(uri); for (IProxyData proxyData : proxies) { switch (proxyData.getType()) { case IProxyData.HTTPS_PROXY_TYPE: case IProxyData.HTTP_PROXY_TYPE: return new Proxy( Type.HTTP, new InetSocketAddress(proxyData.getHost(), proxyData.getPort())); case IProxyData.SOCKS_PROXY_TYPE: return new Proxy( Type.SOCKS, new InetSocketAddress(proxyData.getHost(), proxyData.getPort())); default: logger.warning("Unknown proxy-data type: " + proxyData.getType()); //$NON-NLS-1$ break; } } return Proxy.NO_PROXY; }
Example #8
Source File: p.java From letv with Apache License 2.0 | 6 votes |
private static HttpURLConnection a(Context context, String str) { try { URL url = new URL(str); if (context.getPackageManager().checkPermission(z[43], context.getPackageName()) == 0) { NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService(z[44])).getActiveNetworkInfo(); if (!(activeNetworkInfo == null || activeNetworkInfo.getType() == 1)) { String extraInfo = activeNetworkInfo.getExtraInfo(); if (extraInfo != null && (extraInfo.equals(z[40]) || extraInfo.equals(z[41]) || extraInfo.equals(z[42]))) { return (HttpURLConnection) url.openConnection(new Proxy(Type.HTTP, new InetSocketAddress(z[45], 80))); } } } return (HttpURLConnection) url.openConnection(); } catch (MalformedURLException e) { e.printStackTrace(); return null; } catch (IOException e2) { e2.printStackTrace(); return null; } }
Example #9
Source File: Proxy.java From egdownloader with GNU General Public License v2.0 | 6 votes |
public static void init(boolean useProxy_, String type_, String ip_, String port_, String username_, String pwd_){ useProxy = useProxy_; ip = ip_; port = port_; username = username_; pwd = pwd_; if("ie".equals(type_)){ useIEProxy = true; }else{ useIEProxy = false; if("http".equals(type_)){ type = java.net.Proxy.Type.HTTP; } //jdk6的socks4代理存在bug else if("socks".equals(type_)){ type = java.net.Proxy.Type.SOCKS; } //proxy(); } }
Example #10
Source File: Proxy.java From egdownloader with GNU General Public License v2.0 | 6 votes |
public static java.net.Proxy getNetProxy(){ if(useProxy){ if(useIEProxy){ String[] ieproxys = getIEProxy(); if(ieproxys != null){ //取第一个代理设置 String[] arr = ieproxys[0].split("="); if(arr.length == 2){ Type type = null; String[] hostport = arr[1].split(":"); if(hostport.length == 2){ if(arr[0].equals("http")){ type = java.net.Proxy.Type.HTTP; }else if(arr[0].equals("socks")){ type = java.net.Proxy.Type.SOCKS; } return new java.net.Proxy(type, new InetSocketAddress(hostport[0], Integer.parseInt(hostport[1]))); } } } }else{ return new java.net.Proxy(type, new InetSocketAddress(ip, Integer.parseInt(port))); } } return null; }
Example #11
Source File: ProxyConfig.java From importer-exporter with Apache License 2.0 | 5 votes |
public Proxy toProxy() { if (hasValidProxySettings()) { switch (type) { case HTTP: case HTTPS: return new Proxy(Type.HTTP, new InetSocketAddress(host, port)); case SOCKS: return new Proxy(Type.SOCKS, new InetSocketAddress(host, port)); } } return null; }
Example #12
Source File: PooledSFTPConnection.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
private Proxy[] getProxies() { String proxyString = authority.getProxyString(); if (!StringUtils.isEmpty(proxyString)) { java.net.Proxy proxy = FileUtils.getProxy(authority.getProxyString()); if ((proxy != null) && (proxy.type() != Type.DIRECT)) { URI proxyUri = URI.create(proxyString); String hostName = proxyUri.getHost(); int port = proxyUri.getPort(); String userInfo = proxyUri.getUserInfo(); org.jetel.util.protocols.UserInfo proxyCredentials = null; if (userInfo != null) { proxyCredentials = new org.jetel.util.protocols.UserInfo(userInfo); } switch (proxy.type()) { case HTTP: ProxyHTTP proxyHttp = (port >= 0) ? new ProxyHTTP(hostName, port) : new ProxyHTTP(hostName); if (proxyCredentials != null) { proxyHttp.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword()); } return new Proxy[] {proxyHttp}; case SOCKS: ProxySOCKS4 proxySocks4 = (port >= 0) ? new ProxySOCKS4(hostName, port) : new ProxySOCKS4(hostName); ProxySOCKS5 proxySocks5 = (port >= 0) ? new ProxySOCKS5(hostName, port) : new ProxySOCKS5(hostName); if (proxyCredentials != null) { proxySocks4.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword()); proxySocks5.setUserPasswd(proxyCredentials.getUser(), proxyCredentials.getPassword()); } return new Proxy[] {proxySocks5, proxySocks4}; case DIRECT: return new Proxy[1]; } } } return new Proxy[1]; }
Example #13
Source File: ProxyConfigurationTest.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 5 votes |
public void testGetProxy() { testGetProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("hostname.com", 8080)), "proxy://hostname.com:8080"); testGetProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("hostname.com", 8080)), "proxy://hostname.com"); // default port testGetProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("hostname", 3128)), "proxy://user:p%40ssword@hostname:3128"); testGetProxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("hostname.com", 8080)), "proxysocks://hostname.com:8080"); testGetProxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("hostname.com", 8080)), "proxysocks://hostname.com"); // default port testGetProxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("hostname", 3128)), "proxysocks://user:p%40ssword@hostname:3128"); testGetProxy(Proxy.NO_PROXY, "direct:"); testGetProxy(null, ""); testGetProxy(null, null); testGetProxy(null, "ftp://hostname.com"); }
Example #14
Source File: MasterServerConfig.java From seventh with GNU General Public License v2.0 | 5 votes |
/** * @return the proxy settings */ public Proxy getProxy() { LeoObject proxy = this.config.get("master_server", "proxy_settings"); if(LeoObject.isTrue(proxy)) { Proxy p = new Proxy(Type.HTTP, new InetSocketAddress(this.config.getString("master_server", "proxy_settings", "address"), this.config.getInt(88, "master_server", "proxy_settings", "port"))); return (p); } return Proxy.NO_PROXY; }
Example #15
Source File: Client.java From tinify-java with MIT License | 5 votes |
private Proxy createProxyAddress(final URL proxy) { if (proxy == null) return null; String host = proxy.getHost(); int port = proxy.getPort(); if (port < 0) { port = proxy.getDefaultPort(); } return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); }
Example #16
Source File: RequestFactoryLiveTest.java From tutorials with MIT License | 5 votes |
@Before public void setUp() { Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(PROXY_SERVER_HOST, PROXY_SERVER_PORT)); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setProxy(proxy); restTemplate = new RestTemplate(requestFactory); }
Example #17
Source File: HttpUtil.java From socialauth with MIT License | 5 votes |
/** * * Sets the proxy host and port. This will be implicitly called if * "proxy.host" and "proxy.port" properties are given in properties file * * @param host * proxy host * @param port * proxy port */ public static void setProxyConfig(final String host, final int port) { if (host != null) { int proxyPort = port; if (proxyPort < 0) { proxyPort = 0; } LOG.debug("Setting proxy - Host : " + host + " port : " + port); proxyObj = new Proxy(Type.HTTP, new InetSocketAddress(host, port)); } }
Example #18
Source File: DefaultClientTest.java From feign with Apache License 2.0 | 5 votes |
/** * Test that the proxy is being used, but don't check the credentials. Credentials can still be * used, but they must be set using the appropriate system properties and testing that is not what * we are looking to do here. */ @Test public void canCreateWithImplicitOrNoCredentials() throws Exception { Proxied proxied = new Proxied( TrustingSSLSocketFactory.get(), null, new Proxy(Type.HTTP, proxyAddress)); assertThat(proxied).isNotNull(); assertThat(proxied.getCredentials()).isNullOrEmpty(); /* verify that the proxy */ HttpURLConnection connection = proxied.getConnection(new URL("http://www.example.com")); assertThat(connection).isNotNull().isInstanceOf(HttpURLConnection.class); }
Example #19
Source File: DefaultClientTest.java From feign with Apache License 2.0 | 5 votes |
@Test public void canCreateWithExplicitCredentials() throws Exception { Proxied proxied = new Proxied( TrustingSSLSocketFactory.get(), null, new Proxy(Type.HTTP, proxyAddress), "user", "password"); assertThat(proxied).isNotNull(); assertThat(proxied.getCredentials()).isNotBlank(); HttpURLConnection connection = proxied.getConnection(new URL("http://www.example.com")); assertThat(connection).isNotNull().isInstanceOf(HttpURLConnection.class); }
Example #20
Source File: JaxWsProxySelectorTest.java From aw-reporting with Apache License 2.0 | 5 votes |
/** * Tests select(URI) HTTP */ @Test public void testSelect_HTTP() throws UnknownHostException { systemProperties.setProperty(HTTP_PROXY_HOST, LOCALHOST); systemProperties.setProperty(HTTP_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testHttpUri); assertEquals(list.get(0), new Proxy(Type.HTTP, new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
Example #21
Source File: JaxWsProxySelectorTest.java From aw-reporting with Apache License 2.0 | 5 votes |
/** * Tests select(URI) HTTS */ @Test public void testSelect_HTTPS() throws UnknownHostException { systemProperties.setProperty(HTTPS_PROXY_HOST, LOCALHOST); systemProperties.setProperty(HTTPS_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testHttpsUri); assertEquals(list.get(0), new Proxy(Type.HTTP , new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
Example #22
Source File: JaxWsProxySelectorTest.java From aw-reporting with Apache License 2.0 | 5 votes |
/** * Tests select(URI) FTP */ @Test public void testSelect_FTP() throws UnknownHostException { systemProperties.setProperty(FTP_PROXY_HOST, LOCALHOST); systemProperties.setProperty(FTP_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testFtpUri); assertEquals(list.get(0), new Proxy(Type.HTTP , new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
Example #23
Source File: JaxWsProxySelectorTest.java From aw-reporting with Apache License 2.0 | 5 votes |
/** * Tests select(URI) SOCKS */ @Test public void testSelect_Socks() throws UnknownHostException { systemProperties.setProperty(SOCKS_PROXY_HOST, LOCALHOST); systemProperties.setProperty(SOCKS_PROXY_PORT, TEST_PORT_STR); JaxWsProxySelector ps = new JaxWsProxySelector(ProxySelector.getDefault(), systemProperties); ProxySelector.setDefault(ps); List<Proxy> list = ps.select(testSocksUri); assertEquals(list.get(0), new Proxy(Type.SOCKS , new InetSocketAddress(InetAddress.getByName(LOCALHOST), TEST_PORT_NUM))); }
Example #24
Source File: MojangSkinApi.java From ChangeSkin with MIT License | 5 votes |
public MojangSkinApi(Logger logger, int rateLimit, Collection<HostAndPort> proxies) { this.logger = logger; this.rateLimiter = new RateLimiter(Duration.ofMinutes(10), Math.max(rateLimit, 600)); Set<Proxy> proxyBuilder = proxies.stream() .map(proxy -> new InetSocketAddress(proxy.getHostText(), proxy.getPort())) .map(sa -> new Proxy(Type.HTTP, sa)) .collect(toSet()); this.proxies = Iterables.cycle(proxyBuilder).iterator(); }
Example #25
Source File: PacProxySelector.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private static Proxy proxyFromHostPort(Proxy.Type type, String hostPortString) { try { String[] hostPort = hostPortString.split(":"); String host = hostPort[0]; int port = Integer.parseInt(hostPort[1]); return new Proxy(type, InetSocketAddress.createUnresolved(host, port)); } catch (NumberFormatException|ArrayIndexOutOfBoundsException e) { Log.d(TAG, "Unable to parse proxy " + hostPortString + " " + e); return null; } }
Example #26
Source File: JGoogleAnalyticsTracker.java From ForgeWurst with GNU General Public License v3.0 | 5 votes |
/** * Define the proxy to use for all GA tracking requests. * <p> * Call this static method early (before creating any tracking requests). * * @param proxyAddr * "addr:port" of the proxy to use; may also be given as URL * ("http://addr:port/"). */ public static void setProxy(String proxyAddr) { if(proxyAddr != null) { // Split into "proxyAddr:proxyPort". proxyAddr = null; int proxyPort = 8080; try(Scanner s = new Scanner(proxyAddr)) { s.findInLine("(http://|)([^:/]+)(:|)([0-9]*)(/|)"); MatchResult m = s.match(); if(m.groupCount() >= 2) proxyAddr = m.group(2); if(m.groupCount() >= 4 && !(m.group(4).length() == 0)) proxyPort = Integer.parseInt(m.group(4)); } if(proxyAddr != null) { SocketAddress sa = new InetSocketAddress(proxyAddr, proxyPort); setProxy(new Proxy(Type.HTTP, sa)); } } }
Example #27
Source File: Processor.java From neoscada with Eclipse Public License 1.0 | 5 votes |
private HttpURLConnection openConnection ( final URL url ) throws IOException { final String host = this.properties.getProperty ( "local.proxy.host" ); final String port = this.properties.getProperty ( "local.proxy.port" ); if ( host != null && port != null && !host.isEmpty () && !port.isEmpty () ) { final Proxy proxy = new Proxy ( Type.HTTP, new InetSocketAddress ( host, Integer.parseInt ( port ) ) ); return (HttpURLConnection)url.openConnection ( proxy ); } else { return (HttpURLConnection)url.openConnection (); } }
Example #28
Source File: Processor.java From neoscada with Eclipse Public License 1.0 | 5 votes |
private HttpURLConnection openConnection ( final URL url ) throws IOException { final String host = this.properties.getProperty ( "local.proxy.host" ); final String port = this.properties.getProperty ( "local.proxy.port" ); if ( host != null && port != null && !host.isEmpty () && !port.isEmpty () ) { final Proxy proxy = new Proxy ( Type.HTTP, new InetSocketAddress ( host, Integer.parseInt ( port ) ) ); return (HttpURLConnection)url.openConnection ( proxy ); } else { return (HttpURLConnection)url.openConnection (); } }
Example #29
Source File: WebUtils.java From alipay-sdk with Apache License 2.0 | 5 votes |
private static HttpURLConnection getConnection(URL url, String method, String ctype, String proxyHost, int proxyPort) throws IOException { Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); return getConnection(url, method, ctype, proxy); }
Example #30
Source File: TimeoutAwareConnectionFactoryTest.java From google-cloud-eclipse with Apache License 2.0 | 5 votes |
@Test public void testConstructorWithProxy() throws MalformedURLException, IOException { Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyServer.getHostname(), proxyServer.getPort())); HttpURLConnection connection = new TimeoutAwareConnectionFactory(proxy).openConnection(new URL(NONEXISTENTDOMAIN)); connectReadAndDisconnect(connection); }