static String fetchString()

in aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/plugins/MetadataUtils.java [40:84]


    static String fetchString(String httpMethod, URL url, String token, boolean includeTtl, String metadataService) {
        final HttpURLConnection connection;
        try {
            connection = (HttpURLConnection) url.openConnection();
        } catch (Exception e) {
            logger.debug("Error connecting to " + metadataService, e);
            return "";
        }

        try {
            connection.setRequestMethod(httpMethod);
        } catch (ProtocolException e) {
            logger.warn("Unknown HTTP method, this is a programming bug.", e);
            return "";
        }

        connection.setConnectTimeout(CONNECT_TIMEOUT_MILLIS);
        connection.setReadTimeout(READ_TIMEOUT_MILLIS);

        if (includeTtl) {
            connection.setRequestProperty("X-aws-ec2-metadata-token-ttl-seconds", "60");
        }
        if (token != null && !token.isEmpty()) {
            connection.setRequestProperty("X-aws-ec2-metadata-token", token);
        }

        final int responseCode;
        try {
            responseCode = connection.getResponseCode();
        } catch (Exception e) {
            if (e instanceof SocketTimeoutException) {
                logger.debug("Timed out trying to connect to " + metadataService);
            } else {
                logger.debug("Error connecting to " + metadataService, e);
            }
            return "";
        }

        if (responseCode != 200) {
            logger.warn("Error response from " + metadataService + ": code (" +
                responseCode + ") text " + readResponseString(connection));
        }

        return MetadataUtils.readResponseString(connection).trim();
    }