private static void ResponseEmit(HttpContext context, SoapResponse response) { context.Response.TrySkipIisCustomErrors = true; if (response.IsFault == true) { context.Response.StatusCode = 500; } else { context.Response.StatusCode = 200; } context.Response.CacheControl = "private"; if (response.Version == SoapVersion.Soap12) { context.Response.ContentType = "text/xml; charset=utf-8"; } else { context.Response.ContentType = "application/soap+xml; charset=utf-8"; } context.Response.Write(response.Message); }
public void ParseResponse_IncludeOneValue_ValuePresent() { SoapResponseParser parser = new SoapResponseParser(); const string xml = "<?xml version=\"1.0\"?>" + "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope/\" soap:encodingStyle=\"http://www.w3.org/2003/05/soap-encoding\">" + "<soap:Body>" + "<m:GetPriceResponse xmlns:m=\"http://www.w3schools.com/prices\">" + "<m:Price>1.90</m:Price>" + "</m:GetPriceResponse>" + "</soap:Body>" + "</soap:Envelope>"; SoapResponse response = parser.ParseResponse(xml); Assert.Equal(1, response.Values.Count); }
/// <summary> /// Send SOAP with the following format /// </summary> /// <param name="body"></param> /// <param name="webServiceUri"></param> /// <param name="soapAction"></param> /// <param name="header"></param> /// <param name="host"></param> /// <param name="httpMethod"></param> /// <param name="contentType"></param> /// <param name="httpVersion"></param> /// <param name="throwExceptionIfHttpException"></param> /// <remarks> /// /// POST (@webServiceUri) HTTP/(@version) /// Host: (@webServiceUri.Host or @host) /// Content-Type: (@contentType) /// Content-Length: length /// SOAPAction: "(@soapAction)" /// /// <?xml version="1.0" encoding="utf-8"?> /// <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> /// <soap:Header> /// (@header) /// </soap:Header> /// <soap:Body> /// (@body) /// </soap:Body> /// </soap:Envelope> /// /// </remarks> /// <returns></returns> public static SoapResponse Send(SoapRequest soapRequest, bool throwExceptionIfHttpException = true) { var request = soapRequest.PreparHttpWebRequest(); // this just prepare the HttpHeaders var buffer = soapRequest.HttpBytes; // get the byte[] for Soap message var requestStream = request.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); var response = (HttpWebResponse)request.GetResponse(); // response var result = new SoapResponse(response); if (throwExceptionIfHttpException && result.HttpStatusCodeNumber >= 400) { throw new WhiteMarsException(string.Format("Http Exception: {0} - {1}", result.HttpStatusCodeNumber, result.HttpStatusDescription)); } return result; }
/// <summary> /// Send SOAP with the following format /// </summary> /// <param name="body"></param> /// <param name="webServiceUri"></param> /// <param name="soapAction"></param> /// <param name="header"></param> /// <param name="host"></param> /// <param name="httpMethod"></param> /// <param name="contentType"></param> /// <param name="httpVersion"></param> /// <param name="throwExceptionIfHttpException"></param> /// <remarks> /// /// POST (@webServiceUri) HTTP/(@version) /// Host: (@webServiceUri.Host or @host) /// Content-Type: (@contentType) /// Content-Length: length /// SOAPAction: "(@soapAction)" /// /// <?xml version="1.0" encoding="utf-8"?> /// <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> /// <soap:Header> /// (@header) /// </soap:Header> /// <soap:Body> /// (@body) /// </soap:Body> /// </soap:Envelope> /// /// </remarks> /// <returns></returns> public static SoapResponse Send(SoapRequest soapRequest, bool throwExceptionIfHttpException = true) { var request = soapRequest.PreparHttpWebRequest(); // this just prepare the HttpHeaders var buffer = soapRequest.HttpBytes; // get the byte[] for Soap message var requestStream = request.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); var response = (HttpWebResponse)request.GetResponse(); // response var result = new SoapResponse(response); if (throwExceptionIfHttpException && result.HttpStatusCodeNumber >= 400) { throw new WhiteMarsException(string.Format("Http Exception: {0} - {1}", result.HttpStatusCodeNumber, result.HttpStatusDescription)); } return(result); }
private static SoapResponse ToFault(SoapVersion version, ExecutionHeader execution, ActorException exception) { #region Validations if (execution == null) { throw new ArgumentNullException(nameof(execution)); } if (exception == null) { throw new ArgumentNullException(nameof(exception)); } #endregion /* * */ XNamespace fwkNs = "https://github.com/filipetoscano/Zinc"; XNamespace soapNs; if (version == SoapVersion.Soap11) { soapNs = "http://schemas.xmlsoap.org/soap/envelope/"; } else { soapNs = "http://www.w3.org/2003/05/soap-envelope"; } XDocument responseDoc = new XDocument(); var envelope = new XElement(soapNs + "Envelope"); envelope.Add(new XAttribute(XNamespace.Xmlns + "soap", soapNs.NamespaceName)); var header = new XElement(soapNs + "Header"); var body = new XElement(soapNs + "Body"); var fault = new XElement(soapNs + "Fault"); body.Add(fault); envelope.Add(header, body); responseDoc.Add(envelope); /* * Header */ execution.MomentEnd = DateTime.UtcNow; XmlSerializer serHeader = new XmlSerializer(typeof(ExecutionHeader)); header.Add(serHeader.SerializeAsXElement(execution)); /* * Fault */ if (version == SoapVersion.Soap11) { // TODO: recurse through exception stack XElement x = new XElement(fwkNs + "ActorFault", new XElement(fwkNs + "Actor", new XText(exception.Actor)), new XElement(fwkNs + "Code", new XText(exception.Code.ToString(CultureInfo.InvariantCulture))), new XElement(fwkNs + "Message", new XText(exception.Description)), new XElement(fwkNs + "ExceptionType", new XText(exception.GetType().FullName)) ); if (exception.StackTrace != null) { x.Add(new XElement(fwkNs + "StackTrace", new XText(exception.StackTrace))); } fault.Add( new XElement("faultcode", new XText(exception.Actor.EndsWith("Client", StringComparison.Ordinal) == true ? "soap:Client" : "soap:Server")), new XElement("faultstring", new XText(exception.Description)), new XElement("detail", x) ); } else { // TODO: implemented throw new NotImplementedException(); } /* * */ SoapResponse response = new SoapResponse(); response.Version = version; response.IsFault = true; response.Message = responseDoc.ToString(SaveOptions.DisableFormatting); return(response); }
private static SoapResponse ToResponse(SoapVersion version, ExecutionHeader execution, object response, string serviceNs) { /* * */ XNamespace soapNs; if (version == SoapVersion.Soap11) { soapNs = "http://schemas.xmlsoap.org/soap/envelope/"; } else { soapNs = "http://www.w3.org/2003/05/soap-envelope"; } /* * */ XDocument responseDoc = new XDocument(); var envelope = new XElement(soapNs + "Envelope"); envelope.Add(new XAttribute(XNamespace.Xmlns + "soap", soapNs.NamespaceName)); var header = new XElement(soapNs + "Header"); var body = new XElement(soapNs + "Body"); envelope.Add(header, body); responseDoc.Add(envelope); /* * */ execution.MomentEnd = DateTime.UtcNow; XmlSerializer serHeader = new XmlSerializer(typeof(ExecutionHeader)); header.Add(serHeader.SerializeAsXElement(execution)); /* * */ Type t = response.GetType(); XmlSerializer serBody = XmlSerializerFor(t, serviceNs); body.Add(serBody.SerializeAsXElement(response)); /* * */ SoapResponse resp = new SoapResponse(); resp.Version = version; resp.IsFault = false; resp.Message = responseDoc.ToString(SaveOptions.DisableFormatting); return(resp); }