/// <summary> /// Constructs a SOAP envelope request, with a body that includes the operation as element and the W3C Document and saves the SDMX Part of the response to the specified ouput /// The W3C Document contains either a SDMX-ML Query or a SDMX-ML Registry Interface /// </summary> /// <param name="request"> /// The W3C Document representation of a SDMX-ML Query or QueryStructureRequest /// </param> /// <param name="webServiceOperation"> /// The Web Service function /// </param> /// <param name="tempFileName"> /// The temporary file name /// </param> /// <exception cref="ArgumentNullException"> /// request is null /// </exception> /// <exception cref="NsiClientException"> /// Error in server response or communication /// </exception> private void SendRequest(XmlDocument request, SDMXWSFunctionV21 webServiceOperation, string tempFileName) { if (request == null) { throw new ArgumentNullException("request"); } NsiClientHelper.LogSdmx(_config, request); var sb = new StringBuilder(); sb.AppendFormat(SoapConstants.SoapRequest, this._config.Prefix, this._wsdlConfig.GetTargetNamespace()); var doc = new XmlDocument(); doc.LoadXml(sb.ToString()); string operationName = webServiceOperation.ToString(); XmlNodeList nodes = doc.GetElementsByTagName(SoapConstants.Body, SoapConstants.Soap11Ns); XmlElement operation = doc.CreateElement( this._config.Prefix, operationName, this._wsdlConfig.GetTargetNamespace()); XmlElement queryParent = operation; string parameterName = this._wsdlConfig.GetParameterName(operationName); if (!string.IsNullOrEmpty(parameterName)) { queryParent = doc.CreateElement( this._config.Prefix, parameterName, this._wsdlConfig.GetTargetNamespace()); operation.AppendChild(queryParent); } if (request.DocumentElement != null) { XmlNode sdmxQueryNode = doc.ImportNode(request.DocumentElement, true); queryParent.AppendChild(sdmxQueryNode); } nodes[0].AppendChild(operation); var endpointUri = new Uri(this._config.EndPoint); var webRequest = (HttpWebRequest)WebRequest.Create(endpointUri); webRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; string soapAction = this._wsdlConfig.GetSoapAction(operationName); if (soapAction != null) { webRequest.Headers.Add(SoapConstants.SoapAction, soapAction); } webRequest.ContentType = HttpConstants.Content; // webRequest.Accept = "text/xml"; webRequest.Method = HttpConstants.Post; webRequest.Timeout = 1800 * 1000; // webRequest.CookieContainer = new CookieContainer(); this._config.SetupWebRequestAuth(webRequest); using (Stream stream = webRequest.GetRequestStream()) { doc.Save(stream); } try { using (WebResponse response = webRequest.GetResponse()) { using (Stream stream = response.GetResponseStream()) { if (stream != null) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; using (XmlWriter writer = XmlWriter.Create(tempFileName, settings)) { SoapUtils.ExtractSdmxMessage(stream, writer); } NsiClientHelper.LogSdmx(_config, tempFileName, Resources.InfoSoapResponse); } } } } catch (WebException ex) { NsiClientHelper.HandleSoapFault(ex); } }
/// <summary> /// Constructs a SOAP envelope request, with a body that includes the operation as element and the W3C Document and saves the SDMX Part of the response to the specified ouput /// The W3C Document contains either a SDMX-ML Query or a SDMX-ML Registry Interface /// </summary> /// <param name="request"> /// The W3C Document representation of a SDMX-ML Query or QueryStructureRequest /// </param> /// <param name="tempFileName"> /// The temporary file name /// </param> /// <param name="requestType"> /// The request type /// </param> /// <exception cref="ArgumentNullException"> /// request is null /// </exception> /// <exception cref="NsiClientException"> /// Error in server response or communication /// </exception> private void SendRequest(string request, string tempFileName, RequestType requestType) { if (request == null) { throw new ArgumentNullException("request"); } NsiClientHelper.LogSdmx(this._config, request); var endpointUri = new Uri(this._config.EndPoint); try { using (var writer = File.OpenWrite(tempFileName)) { var client = new RestClient(endpointUri.ToString()); var restRequest = new RestRequest(request, Method.GET); restRequest.AddHeader("Accept-Encoding", "gzip, deflate"); if (requestType == RequestType.Data) { restRequest.AddHeader("Accept", "application/vnd.sdmx.structurespecificdata+xml;version=2.1"); if (Logger.IsDebugEnabled) { var buildUri = client.BuildUri(restRequest); Logger.DebugFormat("Requesting URI : {0}", buildUri); } } this._config.SetupRestAuth(client, restRequest); restRequest.Timeout = 1800 * 1000; // the lambda is executed inside the using // ReSharper disable AccessToDisposedClosure restRequest.ResponseWriter = (responseStream) => responseStream.CopyTo(writer); // ReSharper restore AccessToDisposedClosure var response = client.Execute(restRequest); var httpStatusCode = response.StatusCode; var responseStatus = response.ResponseStatus; var responseUri = response.ResponseUri; Logger.DebugFormat("HTTP status {0}, Response Status {1}, ResponseUri {2}", httpStatusCode, responseStatus, responseUri); // we need to check the status if it is OK or not. No exceptions are thrown. if (httpStatusCode == HttpStatusCode.NotFound || httpStatusCode == HttpStatusCode.Unauthorized) { throw new DataflowException(Resources.NoResultsFound); } if (httpStatusCode != HttpStatusCode.OK) { Logger.ErrorFormat("HTTP status {0}, Response Status {1}, ResponseUri {2}", httpStatusCode, responseStatus, responseUri); Logger.ErrorFormat("ContentType {0}, Content Length: \n{1}", response.ContentType, response.ContentLength); throw new NsiClientException(response.StatusDescription); } } NsiClientHelper.LogSdmx(this._config, tempFileName, Resources.InfoSoapResponse); } catch (Exception ex) { Logger.Error(ex.Message, ex); throw; } }