Exemplo n.º 1
0
        private async Task <XDocument> GetSoapResultAsync(string soapAction, XElement xHeaderData, XElement xBodyData, CancellationToken cancellationToken)
        {
            var xDoc      = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
            var xEnvelope = new XElement(XName.Get("Envelope", NsSoap),
                                         new XAttribute(XName.Get("soap", NsXmlns), NsSoap),
                                         new XAttribute(XName.Get("xsd", NsXmlns), NsXsd),
                                         new XAttribute(XName.Get("xsi", NsXmlns), NsXsi)
                                         );

            xDoc.Add(xEnvelope);
            var xHeader = new XElement(XName.Get("Header", NsSoap), xHeaderData);

            xEnvelope.Add(xHeader);
            var xBody = new XElement(XName.Get("Body", NsSoap), xBodyData);

            xEnvelope.Add(xBody);

            string requestText = XmlUtility.ConvertToString(xDoc, SaveOptions.DisableFormatting);

            if (_logger != null)
            {
                _logger.LogDebug("POST: {0}", _soapLocation);
                _logger.LogDebug("SOAP Action: {0}", soapAction);
                _logger.LogDebug("Request Body:{0}{1}", Environment.NewLine, requestText);
            }

            var request = _net.CreateHttp(_soapLocation);

            request.Method      = "POST";
            request.ContentType = "application/soap+xml; charset=utf-8";
            if (_enableGzipCompression)
            {
                request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
            }
            request.Headers[HttpRequestHeader.Authorization] = $"Bearer {_accessToken}";
            request.Headers[HttpRequestHeader.ContentLength] = Encoding.UTF8.GetByteCount(requestText).ToString();
            request.Headers["SOAPAction"] = soapAction;
            using (var requestStream = await request.GetRequestStreamAsync().ConfigureAwait(false))
                using (var writer = new StreamWriter(requestStream))
                {
                    await writer.WriteAsync(requestText).ConfigureAwait(false);
                }

            cancellationToken.ThrowIfCancellationRequested();

            using (var response = await request.GetResponseAsync().ConfigureAwait(false))
            {
                string responseText = await _net.GetResponseTextAsync(response).ConfigureAwait(false);

                if (_logger != null)
                {
                    _logger.LogDebug("Response status code: {0}", (int)response.StatusCode);
                    _logger.LogDebug("Response body:{0}{1}", Environment.NewLine, responseText);
                }
                return(XDocument.Parse(responseText));
            }
        }
Exemplo n.º 2
0
        public async Task <string> GetContentStringAsync(ReportDefinition definition)
        {
            var xDoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"));
            var xE   = new XElement(XName.Get("reportDefinition", "https://adwords.google.com/api/adwords/cm/v201609"));

            definition.WriteTo(xE);
            xDoc.Add(xE);

            var reportParams = new Dictionary <string, string>();

            reportParams.Add("__rdxml", XmlUtility.ConvertToString(xDoc, SaveOptions.DisableFormatting));

            bool enableGzip = definition.DownloadFormat == DownloadFormat.GzippedCsv || definition.DownloadFormat == DownloadFormat.GzippedXml;

            using (var response = await GetResponseAsync(reportParams, enableGzip).ConfigureAwait(false))
            {
                return(await GetResponseTextAsync(response).ConfigureAwait(false));
            }
        }
Exemplo n.º 3
0
        public void TestConvertToString(XDocument xDoc, SaveOptions saveOptions, string expected)
        {
            string result = XmlUtility.ConvertToString(xDoc, saveOptions);

            Assert.Equal(expected, result);
        }