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:org.globusonline.transfer.JSONTransferAPIClient.java

public Result requestResult(String method, String path, JSONObject data, Map<String, String> queryParams)
        throws IOException, MalformedURLException, GeneralSecurityException, JSONException, APIError {
    String stringData = null;/*from   w w w  . jav  a2 s  . c o m*/
    if (data != null)
        stringData = data.toString();

    HttpsURLConnection c = request(method, path, stringData, queryParams);

    Result result = new Result();
    result.statusCode = c.getResponseCode();
    result.statusMessage = c.getResponseMessage();

    result.document = new JSONObject(readString(c.getInputStream()));

    c.disconnect();

    return result;
}

From source file:it.serverSystem.HttpsTest.java

private void connectUntrusted() throws Exception {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*from  w w  w.jav a 2 s .co  m*/

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };

    // Install the all-trusting trust manager
    // SSLv3 is disabled since SQ 4.5.2 : https://jira.codehaus.org/browse/SONAR-5860
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());

    SSLSocketFactory untrustedSocketFactory = sc.getSocketFactory();

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    URL url = new URL("https://localhost:" + httpsPort + "/sessions/login");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setAllowUserInteraction(true);
    connection.setSSLSocketFactory(untrustedSocketFactory);
    connection.setHostnameVerifier(allHostsValid);

    InputStream input = connection.getInputStream();
    checkCookieFlags(connection);
    try {
        String html = IOUtils.toString(input);
        assertThat(html).contains("<body");
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:com.denimgroup.threadfix.service.remoteprovider.VeracodeRemoteProvider.java

public InputStream getUrl(String urlString, String username, String password) {
    URL url = null;/*from   www.  j ava  2  s  .  c o m*/
    try {
        url = new URL(urlString);
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    }

    HttpsURLConnection m_connect;
    try {
        m_connect = (HttpsURLConnection) url.openConnection();

        setupAuthorization(m_connect, username, password);

        InputStream is = m_connect.getInputStream();

        return is;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.portalblockz.portalbot.urlshorteners.GooGl.java

@Override
public String shorten(String url) {
    StringBuilder response = new StringBuilder();
    try {/*  w  ww .j a v  a 2s.com*/
        URL req = new URL("https://www.googleapis.com/urlshortener/v1/url");

        HttpsURLConnection con = (HttpsURLConnection) req.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes("{\"longUrl\": \"" + url + "\"}");
        wr.flush();
        wr.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject res = new JSONObject(response.toString());
        if (res.optString("id") != null)
            return res.getString("id");

    } catch (Exception ignored) {
        ignored.printStackTrace();
        System.out.print(response.toString());
    }
    return null;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * Query the API for a list of Apps, and the details of each App
 * @return map of App IDs -> App Objects
 *//* w  w w.  jav  a 2s  .  c  o  m*/
public Map<String, App> getAppListing() {
    Map<String, App> map = null;
    try {
        String urlParameters = "?attributes=appName%2CappType%2CappVersions%2CcrashPercent%2Cdau%2Clatency%2C"
                + "latestAppStoreReleaseDate%2ClatestVersionString%2ClinkToAppStore%2CiconURL%2Cmau%2Crating%2Crole";
        HttpsURLConnection conn = sendGetRequest(API_APPS, urlParameters);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        map = mapper.readValue(jp, new TypeReference<Map<String, App>>() {
        });
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return map;
}

From source file:com.github.opengarageapp.GarageService.java

@Override
protected void onHandleIntent(Intent intent) {
    String host = application.getServerHost();
    int port = application.getServerPort();
    Intent broadcast = new Intent(intent.getAction());
    try {//from  w  w w .  j  ava 2 s . co  m
        String protocol = SECURE ? "https" : "http";
        String baseString = protocol + "://" + host + ":" + port + "/openGarageServer/Garage/";
        HttpsURLConnection urlConnection = getRequestFromIntent(baseString, intent);
        int responseCode = urlConnection.getResponseCode();
        application.getAuthenticator().setSuccessfulConnectionMade();
        //getResponseCode or getInputStream signals to actually make the request
        if (responseCode == HttpStatus.SC_OK) {
            String responseBody = convertStreamToString(urlConnection.getInputStream());
            urlConnection.getInputStream().close();
            broadcast.putExtra(EXTRA_HTTP_RESPONSE_TEXT, responseBody);
        } else {
            broadcast.putExtra(EXTRA_HTTP_RESPONSE_TEXT, "");
        }
        Log.i(GarageService.class.getName(), "Response Code: " + responseCode);
        broadcast.putExtra(EXTRA_HTTP_RESPONSE_CODE, responseCode);
    } catch (Exception e) {
        Log.e(getClass().getName(), "Exception", e);
        broadcast.putExtra(EXTRA_HTTP_RESPONSE_CODE, -1);
        broadcast.putExtra(EXTRA_HTTP_RESPONSE_TEXT, e.getMessage());
    } finally {
        sendBroadcast(broadcast);
    }
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID appId (string, optional): The app to retrieve data about,
 * @param metricType The metric to retrieve
 * @param duration can only be 1440 (24 hours) or 43200 (1 month)
 * @param groupBy TODO FILL IN THIS COMMENT
 * @return//from   w  w  w.ja  v  a 2  s. c om
 */
public CrashSummary getErrorPie(String appID, CrashSummary.MetricType metricType, int duration,
        Pie.GroupBy groupBy) {

    String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \""
            + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"" + ",\"groupBy\": \""
            + groupBy.toString() + "\"}" + "}";

    CrashSummary crashSummary = null;
    try {
        HttpsURLConnection conn = sendPostRequest(API_ERROR_PIE, params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class);
        if (crashSummary != null) {
            crashSummary.setParams(appID, metricType, duration);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crashSummary;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID appId (string, optional): The app to retrieve data about,
 * @param metricType The metric to retrieve
 * @param duration can only be 1440 (24 hours) or 43200 (1 month)
 * @return Error object//from ww  w. j  a  v  a 2 s  .  c o m
 */
public CrashSummary getErrorGraph(String appID, CrashSummary.MetricType metricType, int duration) {

    // { "params": {"duration": 43200, "graph": "crashes", "appId": "4f2cc6dfb09315234e000639"}}
    //String responseStr = "{\"data\": {\"start\": \"2014-11-13T00:00:00\", \"interval\": 86400, \"end\": \"2014-12-13T00:00:00\", \"series\": [{\"points\": [0, 3, 2, 0, 2, 3, 2, 3, 0, 6, 0, 0, 0, 1, 0, 0, 6, 2, 0, 1, 0, 0, 4, 1, 0, 0, 1, 2, 0, 0], \"name\": \"crashes\"}]}, \"params\": {\"duration\": 43200, \"graph\": \"crashes\", \"appId\": \"4f2cc6dfb09315234e000639\"}}";

    String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \""
            + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"}" + "}";

    System.out.println(params);
    CrashSummary crashSummary = null;
    try {
        HttpsURLConnection conn = sendPostRequest(API_ERROR_GRAPH, params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class);
        if (crashSummary != null) {
            crashSummary.setParams(appID, metricType, duration);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crashSummary;
}

From source file:com.apteligent.ApteligentJavaClient.java

/**
 * @param appID appId (string, optional): The app to retrieve data about,
 * @param metricType The metric to retrieve
 * @param duration can only be 1440 (24 hours) or 43200 (1 month)
 * @param groupBy TODO FILL IN THIS COMMENT
 * @return/*w ww . j ava  2 s  . c om*/
 */
public CrashSummary getErrorSparklines(String appID, CrashSummary.MetricType metricType, int duration,
        Sparklines.GroupBy groupBy) {

    String params = "{ \"params\": " + "{\"duration\": " + duration + "," + " \"graph\": \""
            + metricType.toString() + "\"," + " \"appId\": \"" + appID + "\"" + ",\"groupBy\": \""
            + groupBy.toString() + "\"}" + "}";

    CrashSummary crashSummary = null;
    try {
        HttpsURLConnection conn = sendPostRequest(API_ERROR_SPARKLINES, params);
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(conn.getInputStream());
        ObjectMapper mapper = getObjectMapper();
        TreeNode node = mapper.readTree(jp);
        crashSummary = mapper.treeToValue(node.get("data"), CrashSummary.class);
        if (crashSummary != null) {
            crashSummary.setParams(appID, metricType, duration);
        }
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    return crashSummary;
}