private HttpResponse doHttpCall()

in sdk/appcenter/src/main/java/com/microsoft/appcenter/http/DefaultHttpClientCallTask.java [158:271]


    private HttpResponse doHttpCall() throws Exception {
        URL url = new URL(mUrl);
        HttpsURLConnection httpsURLConnection = createHttpsConnection(url);
        try {

            /* Build payload now if POST. */
            httpsURLConnection.setRequestMethod(mMethod);
            String payload = null;
            byte[] binaryPayload = null;
            boolean shouldCompress = false;
            boolean isPost = mMethod.equals(METHOD_POST);
            if (isPost && mCallTemplate != null) {

                /* Get bytes, check if large enough to compress. */
                payload = mCallTemplate.buildRequestBody();
                binaryPayload = payload.getBytes(CHARSET_NAME);
                shouldCompress = mCompressionEnabled && binaryPayload.length >= MIN_GZIP_LENGTH;

                /* If no content type specified, assume json. */
                if (!mHeaders.containsKey(CONTENT_TYPE_KEY)) {
                    mHeaders.put(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE);
                }
            }

            /* If about to compress, add corresponding header. */
            if (shouldCompress) {
                mHeaders.put(CONTENT_ENCODING_KEY, CONTENT_ENCODING_VALUE);
            }

            /* Send headers. */
            for (Map.Entry<String, String> header : mHeaders.entrySet()) {
                httpsURLConnection.setRequestProperty(header.getKey(), header.getValue());
            }
            if (isCancelled()) {
                return null;
            }

            /* Call back before the payload is sent. */
            if (mCallTemplate != null) {
                mCallTemplate.onBeforeCalling(url, mHeaders);
            }

            /* Send payload. */
            if (binaryPayload != null) {

                /* Log payload. */
                if (AppCenterLog.getLogLevel() <= Log.VERBOSE) {
                    if (payload.length() < MAX_PRETTIFY_LOG_LENGTH) {
                        payload = TOKEN_REGEX_URL_ENCODED.matcher(payload).replaceAll("token=***");
                        if (CONTENT_TYPE_VALUE.equals(mHeaders.get(CONTENT_TYPE_KEY))) {
                            payload = new JSONObject(payload).toString(2);
                        }
                    }
                    AppCenterLog.verbose(LOG_TAG, payload);
                }

                /* Compress payload if large enough to be worth it. */
                if (shouldCompress) {
                    ByteArrayOutputStream gzipBuffer = new ByteArrayOutputStream(binaryPayload.length);
                    GZIPOutputStream gzipStream = new GZIPOutputStream(gzipBuffer);
                    gzipStream.write(binaryPayload);
                    gzipStream.close();
                    binaryPayload = gzipBuffer.toByteArray();
                }

                /* Send payload on the wire. */
                httpsURLConnection.setDoOutput(true);
                httpsURLConnection.setFixedLengthStreamingMode(binaryPayload.length);
                OutputStream out = httpsURLConnection.getOutputStream();

                //noinspection TryFinallyCanBeTryWithResources
                try {
                    writePayload(out, binaryPayload);
                } finally {
                    out.close();
                }
            }
            if (isCancelled()) {
                return null;
            }

            /* Read response. */
            int status = httpsURLConnection.getResponseCode();
            String response = readResponse(httpsURLConnection);
            if (AppCenterLog.getLogLevel() <= Log.VERBOSE) {
                String contentType = httpsURLConnection.getHeaderField(CONTENT_TYPE_KEY);
                String logPayload;
                if (contentType == null || contentType.startsWith("text/") || contentType.startsWith("application/")) {
                    logPayload = TOKEN_REGEX_JSON.matcher(response).replaceAll("token\":\"***\"");
                    logPayload = REDIRECT_URI_REGEX_JSON.matcher(logPayload).replaceAll("redirect_uri\":\"***\"");
                } else {
                    logPayload = "<binary>";
                }
                AppCenterLog.verbose(LOG_TAG, "HTTP response status=" + status + " payload=" + logPayload);
            }
            Map<String, String> responseHeaders = new HashMap<>();
            for (Map.Entry<String, List<String>> header : httpsURLConnection.getHeaderFields().entrySet()) {
                responseHeaders.put(header.getKey(), header.getValue().iterator().next());
            }
            HttpResponse httpResponse = new HttpResponse(status, response, responseHeaders);

            /* Accept all 2xx codes. */
            if (status >= 200 && status < 300) {
                return httpResponse;
            }

            /* Generate exception on failure. */
            throw new HttpException(httpResponse);
        } finally {

            /* Release connection. */
            httpsURLConnection.disconnect();
        }
    }