Example usage for javax.net.ssl HttpsURLConnection getInputStream

List of usage examples for javax.net.ssl HttpsURLConnection getInputStream

Introduction

In this page you can find the example usage for javax.net.ssl HttpsURLConnection getInputStream.

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:com.yahoo.athenz.example.http.tls.client.HttpTLSClient.java

public static void main(String[] args) {

    // parse our command line to retrieve required input

    CommandLine cmd = parseCommandLine(args);

    final String url = cmd.getOptionValue("url");
    final String keyPath = cmd.getOptionValue("key");
    final String certPath = cmd.getOptionValue("cert");
    final String trustStorePath = cmd.getOptionValue("trustStorePath");
    final String trustStorePassword = cmd.getOptionValue("trustStorePassword");

    // we are going to setup our service private key and
    // certificate into a ssl context that we can use with
    // our http client

    try {//from w w  w  .  ja va  2  s. c om
        KeyRefresher keyRefresher = Utils.generateKeyRefresher(trustStorePath, trustStorePassword, certPath,
                keyPath);
        SSLContext sslContext = Utils.buildSSLContext(keyRefresher.getKeyManagerProxy(),
                keyRefresher.getTrustManagerProxy());

        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
        con.setReadTimeout(15000);
        con.setDoOutput(true);
        con.connect();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            System.out.println("Data output: " + sb.toString());
        }

    } catch (Exception ex) {
        System.out.println("Exception: " + ex.getMessage());
        ex.printStackTrace();
        System.exit(1);
    }
}

From source file:com.cloudbees.tftwoway.Client.java

public static void main(String[] args) throws Exception {
    URL url = new URL(SERVER_ADDRESS);
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

    SSLContext sslContext = createSSLContext();
    connection.setSSLSocketFactory(sslContext.getSocketFactory());

    connection.connect();/*from ww  w  .  ja  v a  2 s .co m*/

    int responseCode = connection.getResponseCode();
    String response = IOUtils.toString(connection.getInputStream(), connection.getContentEncoding());
    System.out.println(responseCode);
    System.out.println(response);
}

From source file:Main.java

/**
 * Make a network request form ONLINE_LOCATION.
 *//*from ww  w .  j  av a2s  .c  om*/
public static boolean makeNetworkCall() {
    try {
        URL url = new URL(ONLINE_LOCATION);
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
        httpsURLConnection.getInputStream();
        Log.d(TAG, "Network call completed.");
        return true;
    } catch (IOException e) {
        Log.e(TAG, "IOException " + e.getMessage());
        return false;
    }
}

From source file:Main.java

/**
 * Saves a file from the given URL using HTTPS to the given filename and returns the file
 * @param link URL to file//from  w  w  w. j ava2s .c om
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static void saveFileFromNetHTTPS(URL link, String fileName) throws IOException {
    HttpsURLConnection con = (HttpsURLConnection) link.openConnection();

    InputStream in = new BufferedInputStream(con.getInputStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File f = new File(fileName);
    if (f.getParentFile() != null) {
        if (f.getParentFile().mkdirs()) {
            System.out.println("Created Directory Structure");
        }
    }

    FileOutputStream fos = new FileOutputStream(f);
    fos.write(response);
    fos.close();
}

From source file:org.apache.hadoop.hbase.http.TestSSLHttpServer.java

private static String readOut(URL url) throws Exception {
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());
    InputStream in = conn.getInputStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copyBytes(in, out, 1024);/* w ww.  j  a v  a2 s  .com*/
    return out.toString();
}

From source file:eu.hansolo.fx.weatherfx.GeoCode.java

/**
 * Returns name of City for given latitude and longitude
 * @param LATITUDE/*from   www  .  j  a v  a 2  s  . co  m*/
 * @param LONGITUDE
 * @return name of City for given latitude and longitude
 */
public static String inverseGeoCode(final double LATITUDE, final double LONGITUDE) {
    String URL_STRING = new StringBuilder(INVERSE_GEO_CODE_URL).append(LATITUDE).append(",").append(LONGITUDE)
            .append("&outFormat=json&thumbMaps=false").toString();

    StringBuilder response = new StringBuilder();
    try {
        final HttpsURLConnection CONNECTION = (HttpsURLConnection) new URL(URL_STRING).openConnection();
        final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream()));
        String inputLine;
        while ((inputLine = IN.readLine()) != null) {
            response.append(inputLine).append("\n");
        }
        IN.close();

        Object obj = JSONValue.parse(response.toString());
        JSONObject jsonObj = (JSONObject) obj;

        JSONArray results = (JSONArray) jsonObj.get("results");
        JSONObject firstResult = (JSONObject) results.get(0);
        JSONArray locations = (JSONArray) firstResult.get("locations");
        JSONObject firstLocation = (JSONObject) locations.get(0);
        return firstLocation.get("adminArea5").toString();
    } catch (IOException ex) {
        System.out.println(ex);
        return "";
    }
}

From source file:eu.hansolo.fx.weatherfx.GeoCode.java

/**
 * Returns a JavaFX Point2D object that contains latitude (y) and longitude(x) of the given
 * Address./*  w w  w  .ja v  a2s.c  o m*/
 * Example format for STREET_CITY_COUNTRY: "1060 W. Addison St., Chicago IL, 60613"
 * @param STREET_CITY_COUNTRY
 * @return a JavaFX Point2D object that contains latitude(y) and longitude(x) of given address
 */
public static Point2D geoCode(final String STREET_CITY_COUNTRY) throws UnsupportedEncodingException {
    String URL_STRING = new StringBuilder(GEO_CODE_URL).append(URLEncoder.encode(STREET_CITY_COUNTRY, "UTF-8"))
            .append("&thumbMaps=false").toString();

    StringBuilder response = new StringBuilder();
    try {
        final HttpsURLConnection CONNECTION = (HttpsURLConnection) new URL(URL_STRING).openConnection();
        final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream()));
        String inputLine;
        while ((inputLine = IN.readLine()) != null) {
            response.append(inputLine).append("\n");
        }
        IN.close();

        Object obj = JSONValue.parse(response.toString());
        JSONObject jsonObj = (JSONObject) obj;

        JSONArray results = (JSONArray) jsonObj.get("results");
        JSONObject firstResult = (JSONObject) results.get(0);
        JSONArray locations = (JSONArray) firstResult.get("locations");
        JSONObject firstLocation = (JSONObject) locations.get(0);
        JSONObject latLng = (JSONObject) firstLocation.get("latLng");
        return new Point2D(Double.parseDouble(latLng.get("lng").toString()),
                Double.parseDouble(latLng.get("lat").toString()));
    } catch (IOException ex) {
        System.out.println(ex);
        return new Point2D(0, 0);
    }
}

From source file:kltn.geocoding.Geocoding.java

private static void prinRaw(String s) throws MalformedURLException, IOException {
    String link = "https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyALCgmmer3Cht-mFQiaJC9yoWdSqvfdAiM";
    link = link + "&address=" + URLEncoder.encode(s);
    URL url = new URL(link);
    HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
    InputStream is = httpsCon.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    String jsonString = writer.toString();
    System.out.println(jsonString);
}

From source file:com.tmobile.TMobileParsing.java

protected static String getDataFromUrl(String url, String basicAuthToken) throws IOException {
    URL urlObj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) urlObj.openConnection();
    con.setRequestProperty("Authorization", "Basic " + basicAuthToken);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String userInfo = in.readLine(); //Successfull invocation of the web service will see JSON in the HTTP response body
    in.close();/*from w  w  w  .j  av a  2  s. c o  m*/

    return userInfo;
}

From source file:kltn.geocoding.Geocoding.java

private static Geometry getBoundary(String s)
        throws MalformedURLException, IOException, org.json.simple.parser.ParseException {
    String link = "https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyALCgmmer3Cht-mFQiaJC9yoWdSqvfdAiM";
    link = link + "&address=" + URLEncoder.encode(s);
    URL url = new URL(link);
    HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
    InputStream is = httpsCon.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");

    String jsonString = writer.toString();
    JSONParser parse = new JSONParser();
    Object obj = parse.parse(jsonString);
    JSONObject jsonObject = (JSONObject) obj;
    System.out.println(s);/*w w w. ja v a  2 s . co  m*/
    System.out.println(jsonObject.toJSONString());
    JSONArray resultArr = (JSONArray) jsonObject.get("results");
    Object resultObject = parse.parse(resultArr.get(0).toString());
    JSONObject resultJsonObject = (JSONObject) resultObject;

    Object geoObject = parse.parse(resultJsonObject.get("geometry").toString());
    JSONObject geoJsonObject = (JSONObject) geoObject;

    if (!geoJsonObject.containsKey("bounds")) {
        return null;
    }
    Object boundObject = parse.parse(geoJsonObject.get("bounds").toString());
    JSONObject boundJsonObject = (JSONObject) boundObject;
    //        System.out.println(boundJsonObject.toJSONString());

    Object southwest = parse.parse(boundJsonObject.get("southwest").toString());
    JSONObject southwestJson = (JSONObject) southwest;
    String southwestLat = southwestJson.get("lat").toString();
    String southwestLong = southwestJson.get("lng").toString();

    Object northeast = parse.parse(boundJsonObject.get("northeast").toString());
    JSONObject northeastJson = (JSONObject) northeast;
    String northeastLat = northeastJson.get("lat").toString();
    String northeastLong = northeastJson.get("lng").toString();

    String polygon = "POLYGON((" + southwestLong.trim() + " " + northeastLat.trim() + "," + northeastLong.trim()
            + " " + northeastLat.trim() + "," + northeastLong.trim() + " " + southwestLat.trim() + ","
            + southwestLong.trim() + " " + southwestLat.trim() + "," + southwestLong.trim() + " "
            + northeastLat.trim() + "))";
    Geometry geo = wktToGeometry(polygon);
    return geo;
}