Skip to content

Instantly share code, notes, and snippets.

@mpoerwito
Last active January 8, 2021 22:47
Show Gist options
  • Select an option

  • Save mpoerwito/403e855b18792256b1c8929c868cdd44 to your computer and use it in GitHub Desktop.

Select an option

Save mpoerwito/403e855b18792256b1c8929c868cdd44 to your computer and use it in GitHub Desktop.
make SOAP POST content and reading SOAP response
using System;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace runthis
{
class Program
{
static void Main(string[] args)
{
//SOAPrequest();
SOAPread();
}
static void SOAPrequest()
{
XDocument content = new XDocument(new XDeclaration("1.0","utf-8", null));
XElement env = new XElement("Envelope");
XElement body = new XElement("Body");
XNamespace xns = "http://tempuri.org/";
XElement action = new XElement(xns + "GET_YEAR");
action.Add(
new XElement(xns + "USERID", _usr),
new XElement(xns + "PASSWORD", _pwd),
new XElement(xns + "SITEID", _siteId),
new XElement(xns + "CATEGORY", "3")
);
body.Add(action);
env.Add(body);
content.Add(env);
StringWriter sw = new Utf8StringWriter();
content.Save(sw, SaveOptions.None);
Console.WriteLine (sw);
}
static void SOAPread()
{
string response = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"<soap:Body><GET_YEARResponse xmlns=\"http://tempuri.org\"><year>2020</year></GET_YEARResponse></soap:Body></soap:Envelope>";
XDocument doc = XDocument.Parse(response);
XNamespace ns = "http://tempuri.org";
IEnumerable<XElement> responses = doc.Descendants(ns + "GET_YEARResponse");
foreach (XElement res in responses)
{
Console.WriteLine((string)res.Element(ns + "year"));
}
}
}
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding { get { return Encoding.UTF8; } }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment