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:org.opensourcetlapp.tl.CustomImageGetter.java

private InputStream getImageInputStream(String url) throws IOException {
    URL myFileUrl;/*from www  .  ja  va2s.c  o m*/
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        myFileUrl = new URL(TLLib.getAbsoluteURL(url));
    }
    HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
    conn.setDoInput(true);
    conn.connect();
    return conn.getInputStream();

}

From source file:org.hawkular.metrics.clients.ptrans.backend.RestForwardingHandlerITest.java

private JsonNode findNumericDataOnServer() throws IOException {
    HttpURLConnection urlConnection = (HttpURLConnection) new URL(findNumericDataUrl).openConnection();
    urlConnection.connect();
    int responseCode = urlConnection.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        return null;
    }//w  ww  .j  a v a 2 s.c om
    ObjectMapper objectMapper = new ObjectMapper();
    try (InputStream inputStream = urlConnection.getInputStream()) {
        return objectMapper.readTree(inputStream);
    }
}

From source file:com.cpp255.bookbarcode.DouBanBookInfoXmlParser.java

/**
 * ?isbn???/* w w  w. ja  v  a  2  s  .  c om*/
 * 
 * @param isbnNo
 * @return
 * @throws IOException
 */
public BookInfo fetchBookInfoByXML(String isbnNo) throws IOException {
    String requestUrl = ISBN_URL + isbnNo;
    URL url = new URL(requestUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.connect();
    if (conn.getResponseCode() == RETURN_BOOKINFO_STATUS) {
        InputStream is = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        br.close();
        return readBookInfo(sb.toString());
    }

    return null;
}

From source file:br.com.arlsoft.pushclient.PushClientModule.java

private static Bitmap getBitmapFromURL(String strURL) {
    URL url = null;// ww  w  .  j  av a 2 s .c  om
    HttpURLConnection connection = null;
    InputStream input = null;
    Bitmap myBitmap = null;
    try {
        url = new URL(strURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        input = connection.getInputStream();
        myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (Exception e) {
        return null;
    }
}

From source file:io.fabric8.itests.basic.cloud.FabricRackspaceContainerTest.java

/**
 * @return the IP address of the client on which this code is running.
 * @throws java.io.IOException/*  w  ww  . j a v a2s.  c o m*/
 */
protected String getOriginatingIp() throws IOException {
    URL url = new URL("http://checkip.amazonaws.com/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    return IOUtils.toString(connection.getInputStream()).trim() + "/32";
}

From source file:net.ae97.pokebot.extensions.scrolls.OnlineCommand.java

@Override
public void runEvent(CommandEvent event) {
    try {/*from   w  w  w  .j  a v a2 s  . com*/
        List<String> lines = new LinkedList<>();
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestProperty("User-Agent", "PokeBot - " + PokeBot.VERSION);
        conn.connect();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(StringUtils.join(lines, "\n"));
        JsonObject obj = element.getAsJsonObject();
        int online = obj.get("data").getAsJsonObject().get("online").getAsInt();
        event.respond("there are " + online + " online users in Scrolls");
    } catch (IOException | JsonSyntaxException ex) {
        PokeBot.getLogger().log(Level.SEVERE, "Error on getting online players for Scrolls", ex);
        event.respond("error on finding online players: " + ex.getLocalizedMessage());
    }
}

From source file:asu.edu.msse.gpeddabu.moviedescriptions.AsyncLibraryConnect.java

private String downloadUrl(String myurl) throws IOException {
    String response = "{}";
    InputStream is = null;/*from www.  ja  va 2  s  .  co m*/
    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        // Starts the query
        conn.connect();
        int responseCode = conn.getResponseCode();
        is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = "";
        StringBuilder responseOutput = new StringBuilder();
        while ((line = br.readLine()) != null) {
            responseOutput.append(line);
        }
        br.close();
        response = responseOutput.toString();
        // Makes sure that the InputStream is closed after the app is
        // finished using it.
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            is.close();
        }
    }
    return response;
}

From source file:com.cloudbees.mtslaves.client.VirtualMachineRef.java

/**
 * Throws away this VM./*from   w  ww.j  a va2s  . c o  m*/
 */
public void dispose() throws IOException, InterruptedException {
    HttpURLConnection con = open("dispose");
    con.setRequestMethod("POST");
    con.connect();
    verifyResponseStatus(con);
    drain(con);
}

From source file:org.pentaho.pat.server.servlet.PentahoServlet.java

private final boolean verifyXmlaUrl(final String xmlaUrl) {
    if (xmlaUrl == null) {
        return false;
    }//from   ww  w  . j ava2  s. co m
    try {
        final URL url = new URL(xmlaUrl);
        // TODO support connection timeout.
        final URLConnection connection = url.openConnection();
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection.setFollowRedirects(true);
            final HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.connect();
            return true;
        } else {
            return false;
        }
    } catch (Throwable t) {
        // TODO log this
        return false;
    }
}

From source file:com.nexmo.sdk.core.client.Client.java

/**
 * Executes a connection, and validates that the body was supplied.
 *
 * @param connection A prepared HttpUrlConnection.
 *
 * @return The response object./*from   w ww.ja v  a 2 s . co  m*/
 * @throws IOException If an error occurs while connecting to the resource.
 * @throws InternalNetworkException If an internal sdk error occurs while parsing the response.
 */
@Override
public Response execute(HttpURLConnection connection) throws IOException, InternalNetworkException {
    try {
        connection.connect();

        if (connection.getResponseCode() == HttpStatus.SC_OK) {
            if (BuildConfig.DEBUG)
                Log.d(TAG, connection.getURL().toString());
            String signatureSupplied = connection.getHeaderField(BaseService.RESPONSE_SIG);
            Response response = new Response(getResponseString(connection.getInputStream()), signatureSupplied);

            if (TextUtils.isEmpty(response.getBody()))
                throw new InternalNetworkException(TAG + "Internal error. Body response missing.");
            else
                return response;
        } else
            throw new InternalNetworkException(
                    TAG + " Internal error. Unable to connect to server. " + connection.getResponseCode());
    } finally {
        connection.disconnect();
    }
}