private static HttpURLConnection getHttpURLConnection(String method, String urlPath, String contentType, String xml)
            throws PagSeguroServiceException {

        log.debug("method: " + method + ", Content-type:" + contentType + ", target URL: " + urlPath);

        URL url;
        HttpURLConnection connection;
        try {

            // Creates a connection
            url = new URL(urlPath);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(method);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Content-type", contentType);
            connection.setRequestProperty("lib-description", "java:" + PagSeguroSystem.getLibversion());
            connection.setRequestProperty("language-engine-description",
                    "java:" + PagSeguroSystem.getLanguageEnginedescription());

            // adding module version to header request
            if (PagSeguroConfig.getModuleVersion() != null) {
                connection.setRequestProperty("module-description", PagSeguroConfig.getModuleVersion());
            }

            // adding cms version to header request
            if (PagSeguroConfig.getCmsVersion() != null) {
                connection.setRequestProperty("cms-description", PagSeguroConfig.getCmsVersion());
            }

            // write the XML
            if (xml != null && !xml.equals("")) {
                log.debug("sending XML: " + xml);
                connection.getOutputStream().write(xml.getBytes(PagSeguroSystem.getPagSeguroEncoding()));
            }

            // handle the response
            int responseCode = connection.getResponseCode();

            if (responseCode != HttpURLConnection.HTTP_OK) {
                if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {

                    List errors = ErrorsParser.readErrosXml(connection.getErrorStream());
                    log(errors);

                    throw new PagSeguroServiceException(errors, HttpError.fromValue(responseCode));
                }

                log.error("HTTP: " + responseCode + ". Error during PagSeguro API call.");
                throw new PagSeguroServiceException(HttpError.fromValue(responseCode));
            }

            log.debug("API call success: " + responseCode);

            // returns the http connection
            return connection;

        } catch (IOException e) {
            log.error("Error while parsing the response", e);
            throw new PagSeguroServiceException(HttpURLConnectionUtil.generateErrorsList(e),
                    HttpError.INTERNAL_SERVER_ERROR);
        } catch (ParserConfigurationException e) {
            log.error("Error while parsing the response", e);
            throw new PagSeguroServiceException(HttpURLConnectionUtil.generateErrorsList(e), HttpError.BAD_REQUEST);
        } catch (SAXException e) {
            log.error("Error while parsing the response", e);
            throw new PagSeguroServiceException(HttpURLConnectionUtil.generateErrorsList(e), HttpError.BAD_REQUEST);
        }

    }