List of usage examples for java.net Proxy Proxy
public Proxy(Type type, SocketAddress sa)
From source file:com.pras.conn._HttpConHandler.java
private Proxy getProxy() { /**//from ww w . j a v a 2s. c o m * Connect through a Proxy */ Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(PROXY_USER, PROXY_PASS.toCharArray()); } }); return new Proxy(Type.HTTP, new InetSocketAddress(PROXY_URL, PROXY_PORT)); // return Proxy.NO_PROXY; }
From source file:org.messic.server.api.musicinfo.duckduckgoimages.MusicInfoDuckDuckGoImages.java
private Proxy getProxy() { if (this.configuration != null) { String url = (String) this.configuration.get(MessicPlugin.CONFIG_PROXY_URL); String port = (String) this.configuration.get(MessicPlugin.CONFIG_PROXY_PORT); if (url != null && port != null && url.length() > 0 && port.length() > 0) { SocketAddress addr = new InetSocketAddress(url, Integer.valueOf(port)); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); return proxy; }//www. jav a 2 s . co m } return null; }
From source file:net.sf.zekr.engine.network.NetworkController.java
public Proxy getProxy(String uri) throws URISyntaxException { Proxy proxy;/* www . j a v a 2 s . com*/ if (SYSTEM_PROXY.equalsIgnoreCase(defaultProxy)) { List<Proxy> proxyList = proxySelector.select(new URI(uri)); proxy = (Proxy) proxyList.get(0); if (proxy.address() == null) { proxy = Proxy.NO_PROXY; } } else if (MANUAL_PROXY.equalsIgnoreCase(defaultProxy)) { SocketAddress sa = InetSocketAddress.createUnresolved(proxyServer, proxyPort); Proxy.Type type = Proxy.Type.HTTP.name().equalsIgnoreCase(proxyType) ? Proxy.Type.HTTP : Proxy.Type.SOCKS.name().equalsIgnoreCase(proxyType) ? Proxy.Type.SOCKS : Proxy.Type.DIRECT; proxy = new Proxy(type, sa); } else { proxy = Proxy.NO_PROXY; } return proxy; }
From source file:Fetcher.Fetcher.java
@Deprecated @Override//from w w w. j a v a 2 s.c o m /** * run() is deprecated. Use startFetching() instead. */ public void run() { WebDocument link = null; HttpURLConnection connection; Proxy p; //PreConnfiguration //Configure proxy //TODO Anonymizer is deprecated. Use in following for warning generation. switch (Variables.anonymizerProxyType) { case DIRECT: p = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort)); break; case HTTP: p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort)); break; case SOCKS: p = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort)); break; case NONE: default: p = null; break; } link = Methods.getNextProfileLink(); while (link != null && isWorking) { //Start fetching ... //Check if it should work or not Date currentTime = Methods.getCurrentTime(); if (!currentTime.after(Variables.startTime) || !currentTime.before(Variables.endTime)) { try { synchronized (t) { getThread().wait(60000); //sleep 60 seconds } } catch (InterruptedException ex) { if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Time is not between start and end time and thread is in exception!"); } } finally { continue; } } String URL = link.getNextUrl(); String UA = Methods.getRandomUserAgent(); //Use this UA for refererd or single links. //loop for referer for (int i = 0; i <= link.getRefererCount(); URL = link.getNextUrl(), i++) { if (Variables.debug && Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Trace, "Fetcher (" + Methods.Colorize(name, Methods.Color.Green) + ") start getting " + URL); } try { //Anonymizer if (Variables.anonymizerProxyType == Variables.AnonymizerProxy.NONE) { connection = (HttpURLConnection) new URL(URL).openConnection(); } else { connection = (HttpURLConnection) new URL(URL).openConnection(p); } connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("User-Agent", UA); connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); connection.setRequestProperty("Accept-Encoding", "gzip, deflated"); String referer = link.getNextReferrer(); if (referer != null) { connection.setRequestProperty("Referer", referer); referer = null; System.gc(); } //Send Cookie using user input if (!(Variables.Cookie == null || Variables.Cookie.equalsIgnoreCase(""))) { connection.setRequestProperty("Cookie", Variables.Cookie); } else if (cookies.getCookieStore().getCookies().size() > 0) { //From referer, there are some cookies connection.setRequestProperty("Cookie", Join(",", cookies.getCookieStore().getCookies())); } connection.setRequestMethod("GET"); connection.connect(); //Get Cookie from response getCookies(connection); if (connection.getResponseCode() == 200) { //Write to file String outputName = Variables.outputDirectory + link.getOutputName().substring(0, link.getOutputName().lastIndexOf(".")) + i + link.getOutputName().substring(link.getOutputName().lastIndexOf(".")); //Check extension if (!(outputName.endsWith("html") || outputName.endsWith("htm"))) { outputName += "html"; } //get content String html = ""; if (connection.getContentEncoding().equalsIgnoreCase("gzip")) { html = IOUtils.toString(new GZIPInputStream(connection.getInputStream())); } else if (connection.getContentEncoding().equalsIgnoreCase("deflate")) { html = IOUtils.toString(new InflaterInputStream(connection.getInputStream())); } FileWriter fw = new FileWriter(outputName); fw.write(html); fw.flush(); fw.close(); } else { //The returned code is not 200. if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Fetcher could not download (" + Methods.Colorize(URL, Methods.Color.Red) + ") in " + name); if (Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Server responded (" + Methods.Colorize(connection.getResponseCode() + " - " + connection.getResponseMessage(), Methods.Color.Red) + ") for " + URL); } } } //Close the connection connection.disconnect(); //Report progress Variables.logger.logResult(connection, link); Methods.oneFinished(); if (Variables.debug && Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Info, "[+] Done fetching (" + Methods.Colorize(URL, Methods.Color.Red) + "]"); } try { synchronized (t) { t.wait(Methods.getNextRandom() * 1000); } } catch (InterruptedException ex) { if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Cannot interrupt thread [" + Methods.Colorize(name, Methods.Color.Red) + "]. Interrupted before!"); } } catch (IllegalArgumentException ex) { if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "-1 is returned as random number for thread [" + Methods.Colorize(name, Methods.Color.Red) + "]."); } } } catch (IOException ex) { if (Variables.debug) { if (Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "] in fetcher (" + Methods.Colorize(name, Methods.Color.Yellow) + ") for writing in (" + Methods.Colorize(link.getOutputName(), Methods.Color.White) + "). Detail:\r\n" + ex.getMessage()); } else { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "]"); } } } catch (NullPointerException ex) { //Thrown sometimes and make the thread as Dead! if (Variables.debug) { if (Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Null pointer occured. Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "] in fetcher (" + Methods.Colorize(name, Methods.Color.Yellow) + ") for writing in (" + Methods.Colorize(link.getOutputName(), Methods.Color.White) + ")."); } else { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Null pointer occured. Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "]"); } } } } //Check size limit and compress ... long size = Methods.getFolderSize(Variables.outputDirectory); if (size >= Variables.outputSizeLimit) { //Deactivate itself by waiting ... Variables.state = Variables.microbotState.Compressing; Variables.threadController.changeActiveThreads(false, t, Variables.microbotState.Compressing); } //Check if user terminated program or not if (isWorking) { link = Methods.getNextProfileLink(); } } //Thread finished. (Normally or by force) Variables.state = Variables.microbotState.Stopping; Variables.threadController.changeActiveThreads(false, t, Variables.microbotState.Stopping); //URLs done. This thread finishes its work. if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Info, "Fetcher (" + Methods.Colorize(name, Methods.Color.Green) + ") finished its work."); } }
From source file:com.github.reverseproxy.ReverseProxyJettyHandler.java
private URLConnection getUrlConnection(HttpServletRequest request, String method, String requestBody, String forwardUrl) throws MalformedURLException, IOException, ProtocolException { final URL url = new URL(forwardUrl); final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); URLConnection urlConnection = url.openConnection(proxy); Map<String, String> requestHeaders = getRequestHeaders(request); requestHeaders.put("Host", urlConnection.getURL().getHost()); ((HttpURLConnection) urlConnection).setRequestMethod(method); if (forwardUrl.startsWith("https")) { SSLSocketFactory sslSocketFactory = null; try {//from w ww. j a v a2s .c om sslSocketFactory = getSSLSocketFactory(); } catch (KeyManagementException e) { throw new IOException("Exception caught while setting up SSL props : ", e); } catch (NoSuchAlgorithmException e) { throw new IOException("Exception caught while setting up SSL props : ", e); } ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslSocketFactory); } urlConnection.setDoInput(true); setHeaders(urlConnection, requestHeaders); setDoOutput(method, requestBody, urlConnection); return urlConnection; }
From source file:com.google.cloud.hadoop.util.HttpTransportFactory.java
/** * Create an {@link HttpTransport} based on an type class and an optional HTTP proxy. * * @param type The type of HttpTransport to use. * @param proxyAddress The HTTP proxy to use with the transport. Of the form hostname:port. If * empty no proxy will be used./*from www . j a v a 2s . com*/ * @return The resulting HttpTransport. * @throws IllegalArgumentException If the proxy address is invalid. * @throws IOException If there is an issue connecting to Google's Certification server. */ public static HttpTransport createHttpTransport(HttpTransportType type, @Nullable String proxyAddress) throws IOException { try { URI proxyUri = parseProxyAddress(proxyAddress); switch (type) { case APACHE: HttpHost proxyHost = null; if (proxyUri != null) { proxyHost = new HttpHost(proxyUri.getHost(), proxyUri.getPort()); } return createApacheHttpTransport(proxyHost); case JAVA_NET: Proxy proxy = null; if (proxyUri != null) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())); } return createNetHttpTransport(proxy); default: throw new IllegalArgumentException(String.format("Invalid HttpTransport type '%s'", type.name())); } } catch (GeneralSecurityException e) { throw new IOException(e); } }
From source file:mdretrieval.FileFetcher.java
public static String[] loadStringFromURL(String destinationURL, boolean acceptRDF) throws IOException { String[] ret = new String[2]; HttpURLConnection urlConnection = null; InputStream inputStream = null; String dest = destinationURL; URL url = new URL(dest); Proxy proxy = null;/* ww w . j av a 2 s .com*/ if (ServerConstants.isProxyEnabled) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ServerConstants.hostname, ServerConstants.port)); urlConnection = (HttpURLConnection) url.openConnection(proxy); } else { urlConnection = (HttpURLConnection) url.openConnection(); } boolean redirect = false; int status = urlConnection.getResponseCode(); if (Master.DEBUG_LEVEL >= Master.LOW) System.out.println("RESPONSE-CODE--> " + status); if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) redirect = true; } if (redirect) { String newUrl = urlConnection.getHeaderField("Location"); dest = newUrl; urlConnection.disconnect(); if (Master.DEBUG_LEVEL > Master.LOW) System.out.println("REDIRECT--> " + newUrl); urlConnection = openMaybeProxyConnection(proxy, newUrl); } try { urlConnection.setRequestMethod("GET"); urlConnection.setRequestProperty("Accept", HTTP_RDFXML_PROP); urlConnection.setDoInput(true); //urlConnection.setDoOutput(true); inputStream = urlConnection.getInputStream(); ret[1] = urlConnection.getHeaderField("Content-Type"); } catch (IllegalStateException e) { if (Master.DEBUG_LEVEL >= Master.LOW) System.out.println(" DEBUG: IllegalStateException"); urlConnection.disconnect(); HttpURLConnection conn2 = openMaybeProxyConnection(proxy, dest); conn2.setRequestMethod("GET"); conn2.setRequestProperty("Accept", HTTP_RDFXML_PROP); conn2.setDoInput(true); inputStream = conn2.getInputStream(); ret[1] = conn2.getHeaderField("Content-Type"); } try { ret[0] = IOUtils.toString(inputStream); if (Master.DEBUG_LEVEL > Master.LOW) { System.out.println(" Content-type: " + urlConnection.getHeaderField("Content-Type")); } } finally { IOUtils.closeQuietly(inputStream); urlConnection.disconnect(); } if (Master.DEBUG_LEVEL > Master.LOW) System.out.println("Done reading " + destinationURL); return ret; }
From source file:org.zalando.stups.spring.http.client.ClientHttpRequestFactorySelector.java
public static ClientHttpRequestFactory getRequestFactory(TimeoutConfig timeoutConfig) { 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 .ja va 2s . c o m if (HTTP_COMPONENTS_AVAILABLE) { HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) HttpComponentsClientRequestFactoryCreator .createRequestFactory(proxyHost, proxyPort); factory.setReadTimeout(timeoutConfig.getReadTimeout()); factory.setConnectTimeout(timeoutConfig.getConnectTimeout()); factory.setConnectionRequestTimeout(timeoutConfig.getConnectionRequestTimeout()); return factory; } else { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(timeoutConfig.getConnectTimeout()); requestFactory.setReadTimeout(timeoutConfig.getReadTimeout()); if (proxyHost != null) { requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))); } return requestFactory; } }
From source file:com.groupon.odo.tests.HttpUtils.java
public static String doProxyHttpsGet(String url, BasicNameValuePair[] data) throws Exception { String fullUrl = url;// w ww. j ava2 s. c om if (data != null) { if (data.length > 0) { fullUrl += "?"; } for (BasicNameValuePair bnvp : data) { fullUrl += bnvp.getName() + "=" + uriEncode(bnvp.getValue()) + "&"; } } TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { } } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { } URL uri = new URL(fullUrl); int port = Utils.getSystemPort(Constants.SYS_FWD_PORT); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port)); URLConnection connection = uri.openConnection(proxy); BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); String accumulator = ""; String line = ""; Boolean firstLine = true; while ((line = rd.readLine()) != null) { accumulator += line; if (!firstLine) { accumulator += "\n"; } else { firstLine = false; } } return accumulator; }
From source file:com.digitalpebble.stormcrawler.protocol.okhttp.HttpProtocol.java
@Override public void configure(Config conf) { super.configure(conf); this.maxContent = ConfUtils.getInt(conf, "http.content.limit", -1); int timeout = ConfUtils.getInt(conf, "http.timeout", 10000); this.completionTimeout = ConfUtils.getInt(conf, "topology.message.timeout.secs", completionTimeout); userAgent = getAgentString(conf);//w w w . j a va2 s . co m okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder().retryOnConnectionFailure(true) .followRedirects(false).connectTimeout(timeout, TimeUnit.MILLISECONDS) .writeTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(timeout, TimeUnit.MILLISECONDS); String proxyHost = ConfUtils.getString(conf, "http.proxy.host", null); int proxyPort = ConfUtils.getInt(conf, "http.proxy.port", 8080); boolean useProxy = proxyHost != null && proxyHost.length() > 0; // use a proxy? if (useProxy) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); builder.proxy(proxy); } client = builder.build(); }