private HttpURLConnection getConn(Request request)
            throws URISyntaxException, IOException, HmacException {
        String path = String.format("%s/%s", request.getPathPrefix(),
                subscriberId);

        if (request.getPackageId() != null) {
            path += "/" + request.getPackageId();
        }

        if (request.getTemplateName() != null) {
            path += "/" + request.getTemplateName();
        }

        // Use the URI constructor to percent-encode the URL
        URL httpsUrl = (new URI("https", address, path, request.getQuery(),
                null)).toURL();

        Proxy httpsProxy = Proxy.NO_PROXY;
        if (proxy != null && !proxy.equals("")) {
            httpsProxy = new Proxy(Proxy.Type.HTTP,
                    new InetSocketAddress(proxy, proxyPort));
        }

        HttpsURLConnection conn = (HttpsURLConnection) httpsUrl
                .openConnection(httpsProxy);
        signAndDate(conn, request.getHmacParams());

        String method = request.getMethod();

        conn.setRequestMethod(method);
        conn.setAllowUserInteraction(false);

        if (areCertErrorsIgnored) {
            Util.setConnToIgnoreHostNameErrors(conn);
        }

        if (method.equals("POST") || method.equals("PUT")) {
            conn.setDoOutput(true);
            InputStream contentStream = request.getContentStream();
            if (contentStream != null) {
                int length = request.getContentStreamGetter().getLength();
                if (length > -1)
                {
                    conn.setFixedLengthStreamingMode(length);
                }
                Util.copyStream(contentStream, conn.getOutputStream());
            } else {
                conn.setFixedLengthStreamingMode(0);
            }
        }

        return conn;
    }