List of usage examples for java.net Proxy Proxy
public Proxy(Type type, SocketAddress sa)
From source file:com.mebigfatguy.polycasso.URLFetcher.java
/** * retrieve arbitrary data found at a specific url * - either http or file urls//www . j a va2s . c om * for http requests, sets the user-agent to mozilla to avoid * sites being cranky about a java sniffer * * @param url the url to retrieve * @param proxyHost the host to use for the proxy * @param proxyPort the port to use for the proxy * @return a byte array of the content * * @throws IOException the site fails to respond */ public static byte[] fetchURLData(String url, String proxyHost, int proxyPort) throws IOException { HttpURLConnection con = null; InputStream is = null; try { URL u = new URL(url); if (url.startsWith("file://")) { is = new BufferedInputStream(u.openStream()); } else { Proxy proxy; if (proxyHost != null) { proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } else { proxy = Proxy.NO_PROXY; } con = (HttpURLConnection) u.openConnection(proxy); con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6"); con.addRequestProperty("Accept-Charset", "UTF-8"); con.addRequestProperty("Accept-Language", "en-US,en"); con.addRequestProperty("Accept", "text/html,image/*"); con.setDoInput(true); con.setDoOutput(false); con.connect(); is = new BufferedInputStream(con.getInputStream()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); return baos.toByteArray(); } finally { IOUtils.closeQuietly(is); if (con != null) { con.disconnect(); } } }
From source file:com.clustercontrol.util.EndpointManager.java
public static void setProxy(String proxyHost, int proxyPort) { Proxy proxy = null;/*w w w . jav a 2 s. co m*/ if (proxyHost == null || proxyHost.length() == 0) { proxy = Proxy.NO_PROXY; } else { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } getInstance().proxy = proxy; }
From source file:mdretrieval.FileFetcher.java
public static InputStream fetchFileFromUrl(String urlString) { InputStream inputStream = null; Proxy proxy = ServerConstants.isProxyEnabled ? new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ServerConstants.hostname, ServerConstants.port)) : Proxy.NO_PROXY;/* w w w. jav a2s. c o m*/ URL url; try { url = new URL(urlString); url.openConnection(); if (proxy != Proxy.NO_PROXY && proxy.type() != Proxy.Type.DIRECT) { inputStream = url.openConnection(proxy).getInputStream(); } else { inputStream = url.openConnection().getInputStream(); } } catch (MalformedURLException e) { GUIrefs.displayAlert("Invalid URL " + urlString + "- \\n malformed expression"); e.printStackTrace(); inputStream = null; } catch (IOException ioe) { GUIrefs.displayAlert("Cannot read from " + StringUtilities.escapeQuotes(urlString) + "- IOException"); ioe.printStackTrace(); inputStream = null; } return inputStream; }
From source file:com.metratech.metanga.api.impl.ClientHttpRequestFactorySelector.java
public static ClientHttpRequestFactory getRequestFactory() { Properties properties = System.getProperties(); String proxyHost = properties.getProperty("http.proxyHost"); int proxyPort = properties.containsKey("http.proxyPort") ? Integer.valueOf(properties.getProperty("http.proxyPort")) : 80;//w ww . j a v a 2s .co m if (HTTP_COMPONENTS_AVAILABLE) { return HttpComponentsClientRequestFactoryCreator.createRequestFactory(proxyHost, proxyPort); } else { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); if (proxyHost != null) { requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))); } return requestFactory; } }
From source file:com.zf.util.Post_NetNew.java
/** * ?// ww w.j a va2s.co m * * @param pams * @param ip * @param port * @return * @throws Exception */ public static String pn(Map<String, String> pams, String ip, int port) throws Exception { if (null == pams) { return ""; } InetSocketAddress addr = new InetSocketAddress(ip, port); Proxy proxy = new Proxy(Type.HTTP, addr); String strtmp = "url"; URL url = new URL(pams.get(strtmp)); pams.remove(strtmp); strtmp = "body"; String body = pams.get(strtmp); pams.remove(strtmp); strtmp = "POST"; if (StringUtils.isEmpty(body)) strtmp = "GET"; HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(proxy); httpConn.setConnectTimeout(30000); httpConn.setReadTimeout(30000); httpConn.setUseCaches(false); httpConn.setRequestMethod(strtmp); for (String pam : pams.keySet()) { httpConn.setRequestProperty(pam, pams.get(pam)); } if ("POST".equals(strtmp)) { httpConn.setDoOutput(true); httpConn.setDoInput(true); DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream()); dos.writeBytes(body); dos.flush(); } int resultCode = httpConn.getResponseCode(); StringBuilder sb = new StringBuilder(); sb.append(resultCode).append("\n"); String readLine; InputStream stream; try { stream = httpConn.getInputStream(); } catch (Exception ignored) { stream = httpConn.getErrorStream(); } try { BufferedReader responseReader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); while ((readLine = responseReader.readLine()) != null) { sb.append(readLine).append("\n"); } } catch (Exception ignored) { } return sb.toString(); }
From source file:io.github.bonigarcia.wdm.Downloader.java
public static Proxy createProxy() { String proxyString = System.getenv("HTTPS_PROXY"); if (proxyString == null || proxyString.length() < 1) proxyString = System.getenv("HTTP_PROXY"); if (proxyString == null || proxyString.length() < 1) { return null; }/*from ww w. ja v a 2s .c o m*/ proxyString = proxyString.replace("http://", ""); proxyString = proxyString.replace("https://", ""); StringTokenizer st = new StringTokenizer(proxyString, ":"); if (st.countTokens() != 2) return null; String host = st.nextToken(); String portString = st.nextToken(); try { int port = Integer.parseInt(portString); return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); } catch (NumberFormatException e) { return null; } }
From source file:info.guardianproject.netcipher.NetCipher.java
/** * Set the global HTTP proxy for all new {@link HttpURLConnection}s and * {@link HttpsURLConnection}s that are created after this is called. * <p>//www .jav a 2 s. c o m * {@link #useTor()} will override this setting. Traffic must be directed * to Tor using the proxy settings, and Orbot has its own proxy settings * for connections that need proxies to work. So if "use Tor" is enabled, * as tested by looking for the static instance of Proxy, then no other * proxy settings are allowed to override the current Tor proxy. * * @param host the IP address for the HTTP proxy to use globally * @param port the port number for the HTTP proxy to use globally */ public static void setProxy(String host, int port) { if (!TextUtils.isEmpty(host) && port > 0) { InetSocketAddress isa = new InetSocketAddress(host, port); setProxy(new Proxy(Proxy.Type.HTTP, isa)); } else if (NetCipher.proxy != ORBOT_HTTP_PROXY) { setProxy(null); } }
From source file:org.springframework.social.openidconnect.api.impl.GAECompatibleClientHttpRequestFactorySelector.java
public static ClientHttpRequestFactory getRequestFactory() { Properties properties = System.getProperties(); String proxyHost = properties.getProperty("http.proxyHost"); int proxyPort = properties.containsKey("http.proxyPort") ? Integer.valueOf(properties.getProperty("http.proxyPort")) : 80;// w w w . j a v a2 s .com if (HTTP_COMPONENTS_AVAILABLE) { HttpClientBuilder httpClientBuilder = HttpClients.custom(); if (proxyHost != null) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClientBuilder.setProxy(proxy); } return HttpComponentsClientRequestFactoryCreator.createRequestFactory(httpClientBuilder.build(), proxyHost, proxyPort); } else { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); if (proxyHost != null) { requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))); } return requestFactory; } }
From source file:com.intuit.tank.rest.BaseRestClient.java
/** * /*from www . ja v a 2s . com*/ * @param serviceUrl */ public BaseRestClient(String serviceUrl, final String proxyServer, final Integer proxyPort) { setBaseUrl(serviceUrl); if (StringUtils.isNotEmpty(proxyServer)) { ConnectorProvider connectorprovider = new HttpUrlConnectorProvider() { private Proxy proxy; private void initializeProxy() { int port = proxyPort != null ? proxyPort : 80; proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyServer, port)); } public HttpURLConnection getHttpURLConnection(URL url) throws IOException { initializeProxy(); return (HttpURLConnection) url.openConnection(proxy); } }; client = ClientBuilder.newBuilder().register(connectorprovider).build(); } else { client = ClientBuilder.newClient(); } // client.setConnectTimeout(5000); // client.setFollowRedirects(true); LOG.info("client for url " + baseUrl + ": proxy=" + (proxyServer != null ? proxyServer + ":" + proxyPort : "none")); }
From source file:se252.jan15.calvinandhobbes.project0.IIScCampusMapRDBMS_GETService.java
private static JSONObject getJSON(String url) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.iisc.ernet.in", 3128)); InputStream is;//w ww .ja v a 2 s . c om try { is = new URL(url).openConnection(proxy).getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); is.close(); return json; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }