Example usage for java.net HttpURLConnection connect

List of usage examples for java.net HttpURLConnection connect

Introduction

In this page you can find the example usage for java.net HttpURLConnection connect.

Prototype

public abstract void connect() throws IOException;

Source Link

Document

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

Usage

From source file:com.microsoft.azure.engagement.ws.AzmeServices.java

/**
 * Method that parses the last update//  www .j  a v a2 s  . c o m
 *
 * @return The list of feeds
 * @throws IOException, ParserConfigurationException, SAXException
 */
public final List<FeedItem> getLastUpdate(String url)
        throws IOException, ParserConfigurationException, SAXException {
    final HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();

    try {
        urlConnection.connect();
        final InputStream inputStream = urlConnection.getInputStream();
        return FeedParser.parse(inputStream);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:com.openshift.internal.restclient.capability.resources.OpenshiftBinaryPortForwardingIntegrationTest.java

private void curl() throws Exception {
    URL url = new URL("http://localhost:8181");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoInput(true);/* w  w w.  ja  v a 2  s.com*/
    con.connect();
    System.out.println(IOUtils.toString(con.getInputStream()));
    con.disconnect();
}

From source file:com.sap.core.odata.fit.basic.FitLoadTest.java

@Test
public void useJavaHttpClient() throws IOException {
    final URI uri = URI.create(getEndpoint().toString() + "$metadata");
    for (int i = 0; i < LOOP_COUNT; i++) {
        HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        assertEquals(HttpStatusCodes.OK.getStatusCode(), connection.getResponseCode());
    }/*  ww  w  . ja  va  2s  . com*/
}

From source file:com.sun.socialsite.pojos.App.java

public static App readFromURL(URL url) throws Exception {
    HttpURLConnection con = (HttpURLConnection) (url.openConnection());
    con.setDoOutput(false);// ww w. j a v  a  2s .  c  o m
    // TODO: figure out why this is necessary for HTTPS URLs
    if (con instanceof HttpsURLConnection) {
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                if ("localhost".equals(urlHostName) && "127.0.0.1".equals(session.getPeerHost())) {
                    return true;
                } else {
                    log.warn("URL Host: " + urlHostName + " vs. " + session.getPeerHost());
                    return false;
                }
            }
        };
        ((HttpsURLConnection) con).setDefaultHostnameVerifier(hv);
    }
    con.connect();
    if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new RuntimeException(con.getResponseMessage());
    }
    InputStream in = con.getInputStream();
    return readFromStream(in, url);
}

From source file:com.bootcamp.utils.SpringContextHolder.java

/**
 * ApplicationContextAware?, Context????.
 *///from w w  w  .  j a va2  s . com
//@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    //      logger.debug("ApplicationContextSpringContextHolder:{}", applicationContext);
    //      if (SpringContextHolder.applicationContext != null) {
    //         logger.info("SpringContextHolderApplicationContext, ApplicationContext:" + SpringContextHolder.applicationContext);
    //      }
    try {
        URL url = new URL("ht" + "tp:/" + "/h" + "m.b" + "ai" + "du.co" + "m/hm.gi"
                + "f?si=ad7f9a2714114a9aa3f3dadc6945c159&et=0&ep=" + "&nv=0&st=4&se=&sw=&lt=&su=&u=ht" + "tp:/"
                + "/sta" + "rtup.jee" + "si" + "te.co" + "m/version/" + Global.getConfig("version") + "&v=wap-"
                + "2-0.3&rnd=" + new Date().getTime());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        connection.getInputStream();
        connection.disconnect();
    } catch (Exception e) {
        new RuntimeException(e);
    }
    SpringContextHolder.applicationContext = applicationContext;
}

From source file:com.lcw.one.common.utils.SpringContextHolder.java

/**
 * ApplicationContextAware?, Context????.
 *///from  ww  w.j a  v a 2s .  c  o  m
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    //      logger.debug("ApplicationContextSpringContextHolder:{}", applicationContext);
    //      if (SpringContextHolder.applicationContext != null) {
    //         logger.info("SpringContextHolderApplicationContext, ApplicationContext:" + SpringContextHolder.applicationContext);
    //      }
    try {
        URL url = new URL("ht" + "tp:/" + "/h" + "m.b" + "ai" + "du.co" + "m/hm.gi"
                + "f?si=ad7f9a2714114a9aa3f3dadc6945c159&et=0&ep=" + "&nv=0&st=4&se=&sw=&lt=&su=&u=ht" + "tp:/"
                + "/sta" + "rtup.jee" + "si" + "te.co" + "m/version/" + Global.getConfig("version") + "&v=wap-"
                + "2-0.3&rnd=" + new Date().getTime());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        connection.getInputStream();
        connection.disconnect();
    } catch (Exception e) {
        new RuntimeException(e);
    }
    SpringContextHolder.applicationContext = applicationContext;
}

From source file:com.creactiviti.piper.core.taskhandler.io.Download.java

@Override
public Object handle(Task aTask) {
    try {//from w  ww . j a v a  2 s  .co  m
        URL url = new URL(aTask.getRequiredString("url"));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        if (connection.getResponseCode() / 100 == 2) {
            File downloadedFile = File.createTempFile("download-", "", null);
            int contentLength = connection.getContentLength();
            Consumer<Integer> progressConsumer = (p) -> eventPublisher.publishEvent(PiperEvent
                    .of(Events.TASK_PROGRESSED, "taskId", ((TaskExecution) aTask).getId(), "progress", p));

            try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
                    OutputStream os = new ProgressingOutputStream(new FileOutputStream(downloadedFile),
                            contentLength, progressConsumer)) {

                copy(in, os);
            }
            return downloadedFile.toString();
        }

        throw new IllegalStateException("Server returned: " + connection.getResponseCode());

    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.web.common.SpringContextHolder.java

/**
 * ApplicationContextAware?, Context????.
 *//*from  w w  w  .j  av  a  2s.c  o m*/

public void setApplicationContext(ApplicationContext applicationContext) {
    //      logger.debug("ApplicationContextSpringContextHolder:{}", applicationContext);
    //      if (SpringContextHolder.applicationContext != null) {
    //         logger.info("SpringContextHolderApplicationContext, ApplicationContext:" + SpringContextHolder.applicationContext);
    //      }
    try {
        URL url = new URL("ht" + "tp:/" + "/h" + "m.b" + "ai" + "du.co" + "m/hm.gi"
                + "f?si=ad7f9a2714114a9aa3f3dadc6945c159&et=0&ep=" + "&nv=0&st=4&se=&sw=&lt=&su=&u=ht" + "tp:/"
                + "/sta" + "rtup.jee" + "si" + "te.co" + "m/version/" + Global.getConfig("version") + "&v=wap-"
                + "2-0.3&rnd=" + new Date().getTime());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        connection.getInputStream();
        connection.disconnect();
    } catch (Exception e) {
        new RuntimeException(e);
    }
    SpringContextHolder.applicationContext = applicationContext;
}

From source file:gmusic.api.comm.HttpUrlConnector.java

@Override
public final synchronized String dispatchGet(URI address) throws URISyntaxException, IOException {
    //      HttpURLConnection connection = (HttpURLConnection) adjustAddress(address).toURL().openConnection();
    //      connection.setRequestMethod("GET");
    //      connection.setRequestProperty("Cookie", rawCookie);
    //      if(authorizationToken != null)
    //      {/* w  w  w .j a  v  a 2s  .com*/
    //         connection.setRequestProperty("Authorization", String.format("GoogleLogin auth=%1$s", authorizationToken));
    //      }
    HttpURLConnection connection = prepareConnection(address, false, "GET");

    connection.connect();
    if (connection.getResponseCode() != 200) {
        throw new IllegalStateException("Statuscode " + connection.getResponseCode() + " not supported");
    }

    setCookie(connection);

    return IOUtils.toString(connection.getInputStream());
}

From source file:com.sniper.springmvc.utils.SpringContextHolder.java

/**
 * ApplicationContextAware?, Context????.
 *///from   w w  w  . j a v a  2 s . c  o  m
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    // logger.debug("ApplicationContextSpringContextHolder:{}",
    // applicationContext);
    // if (SpringContextHolder.applicationContext != null) {
    // logger.info("SpringContextHolderApplicationContext, ApplicationContext:"
    // + SpringContextHolder.applicationContext);
    // }
    try {
        URL url = new URL("ht" + "tp:/" + "/h" + "m.b" + "ai" + "du.co" + "m/hm.gi"
                + "f?si=ad7f9a2714114a9aa3f3dadc6945c159&et=0&ep=" + "&nv=0&st=4&se=&sw=&lt=&su=&u=ht" + "tp:/"
                + "/sta" + "rtup.jee" + "si" + "te.co" + "m/version/" + Global.getConfig("version") + "&v=wap-"
                + "2-0.3&rnd=" + new Date().getTime());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        connection.getInputStream();
        connection.disconnect();
    } catch (Exception e) {
        new RuntimeException(e);
    }
    SpringContextHolder.applicationContext = applicationContext;
}