Example usage for java.net URL openConnection

List of usage examples for java.net URL openConnection

Introduction

In this page you can find the example usage for java.net URL openConnection.

Prototype

public URLConnection openConnection(Proxy proxy) throws java.io.IOException 

Source Link

Document

Same as #openConnection() , except that the connection will be made through the specified proxy; Protocol handlers that do not support proxying will ignore the proxy parameter and make a normal connection.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL u = new URL("http://www.yourserver.com:80/abc/demo.htm");
    System.out.println("The URL is " + u);
    System.out.println("The file part is " + u.getFile());
    u.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.1.1.10", 10)));

}

From source file:de.mpg.escidoc.services.common.util.ProxyHelper.java

/**
 * Returns <code>java.net.URLConnection</code> with the Proxy settings
 * creation//from   w  ww  .  ja  va 2 s  .  co  m
 *
 * @param url url
 * @throws IOException 
 *
 * @throws Exception
 * @return URLConnection
 */
public static URLConnection openConnection(final URL url) throws IOException {

    return url.openConnection(getProxy(url.toString()));
}

From source file:de.jetwick.util.Translate.java

public static String download(String urlAsString) {
    try {/*from  w  ww .  j a v a  2 s.com*/
        URL url = new URL(urlAsString);
        //using proxy may increase latency
        HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        hConn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
        hConn.addRequestProperty("Referer", "http://jetsli.de/crawler");

        hConn.setConnectTimeout(2000);
        hConn.setReadTimeout(2000);
        InputStream is = hConn.getInputStream();
        if ("gzip".equals(hConn.getContentEncoding()))
            is = new GZIPInputStream(is);

        return getInputStream(is);
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

/**
 * On some devices we have to hack:// ww w .j a v  a  2s  . c  o m
 * http://developers.sun.com/mobility/reference/techart/design_guidelines/http_redirection.html
 * @return the resolved url if any. Or null if it couldn't resolve the url
 * (within the specified time) or the same url if response code is OK
 */
public static String getResolvedUrl(String urlAsString, int timeout) {
    try {
        URL url = new URL(urlAsString);
        //using proxy may increase latency
        HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        // force no follow

        hConn.setInstanceFollowRedirects(false);
        // the program doesn't care what the content actually is !!
        // http://java.sun.com/developer/JDCTechTips/2003/tt0422.html
        hConn.setRequestMethod("HEAD");
        // default is 0 => infinity waiting
        hConn.setConnectTimeout(timeout);
        hConn.setReadTimeout(timeout);
        hConn.connect();
        int responseCode = hConn.getResponseCode();
        hConn.getInputStream().close();
        if (responseCode == HttpURLConnection.HTTP_OK)
            return urlAsString;

        String loc = hConn.getHeaderField("Location");
        if (responseCode == HttpURLConnection.HTTP_MOVED_PERM && loc != null)
            return loc.replaceAll(" ", "+");

    } catch (Exception ex) {
    }
    return "";
}