I would recommend looking at the HttpWebRequest
and the HttpWebResponse
classes. The HttpWebRequest
class will allow you to set the body, headers, credentials, and other things for making the request. And the HttpWebResponse
should allow you to read in whatever information you need.
Hope this helps some, but your question is a unclear as to what you need to do. But the following code will download an XML document from a provided endpoint that you have posted the request XML to.
var requestXml = new XmlDocument();
// build XML request
var httpRequest = HttpWebRequest.Create("https://www.website.com/");
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml";
// set appropriate headers
using (var requestStream = httpRequest.GetRequestStream())
{
requestXml.Save(requestStream);
}
using (var response = (HttpWebResponse)httpRequest.GetResponse())
using (var responseStream = response.GetResponseStream())
{
// may want to check response.StatusCode to
// see if the request was successful
var responseXml = new XmlDocument();
responseXml.Load(responseStream);
}
As far as dealing with doing stuff over SSL/TLS there you are in luck, .NET will take care of that for you, you just need to specify https
over http
in the URL that your provide to HttpWebRequest.Create
.
There are a number of ways you construct your XML, one way is to use the XElement
, XAttribute
, and XNamespace
objects within the System.Xml.Linq
namespace. Which unless you cannot use at least .NET 3.5 I would recommend using. Since you are creating the application in APS.NET 4.0 this shouldn't be an issue.
When you create an XElement
and you need to specify a namespace you'll need to first create an XNamespace
object.
var ns = XNamespace.Get("http://tempuri.com");
var root = new XElement(ns + "Root", "Body");
This will result in the following XML:
<Root xmlns="http://tempuri.com">Body</Root>
However, you need to be able to specify a namespace prefix. In order to do this you will need to add an XAttribute
object to the XElement
you first use the prefix on and specify the prefix through the constructor.
var ns = XNamespace.Get("http://tempuri.com");
var root = new XElement(ns + "Root",
new XAttribute(XNamespace.Xmlns + "prefix", ns)
"Body"
);
This will result in the following XML:
<prefix:Root xmlns:prefix="http://tempuri.com">Body</prefix:Root>
Here is a portion of your XML constructed using those objects.
var id = 9000;
var regKey = "RegistrationKey";
var inType = 1;
var prodType = 5;
var tranCode = 1;
var soapNs = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
var v1Ns = XNamespace.Get("http://postilion/realtime/merchantframework/xsd/v1/");
var xml= new XElement(soapNs + "Envelope",
new XAttribute(XNamespace.Xmlns + "soapenv", soapNs),
new XElement(soapNs + "Body",
new XElement(v1Ns + "SendTranRequest",
new XAttribute(XNamespace.Xmlns + "v1", v1Ns),
new XElement(v1Ns + "merc",
new XElement(v1Ns + "id", id),
new XElement(v1Ns + "regKey", regKey),
new XElement(v1Ns + "inType", inType),
new XElement(v1Ns + "prodType", prodType)
),
new XElement(v1Ns + "tranCode", tranCode)
)
)
);
This results in the following XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<v1:SendTranRequest xmlns:v1="http://postilion/realtime/merchantframework/xsd/v1/">
<v1:merc>
<v1:id>9000</v1:id>
<v1:regKey>RegistrationKey</v1:regKey>
<v1:inType>1</v1:inType>
<v1:prodType>5</v1:prodType>
</v1:merc>
<v1:tranCode>1</v1:tranCode>
</v1:SendTranRequest>
</soapenv:Body>
</soapenv:Envelope>
To include the XML into the body of your HttpWebRequest
do:
var xmlWriterSettings = new XmlWriterSettings
{
NewLineHandling = NewLineHandling.None
};
using (var requestStream = httpRequest.GetRequestStream())
using (var writer = XmlWriter.Create(requestStream, xmlWriterSettings))
{
xml.WriteTo(writer);
}
One thing of note here is the creation of the XmlWriter
with the XmlWriterSettings
, this allows you to not include any whitespace between the XML elements. If you need whitespace you can create the XmlWriter
by just calling XmlWriter.Create(stream)
If you cannot use .NET 3.5 or higher, you can use the XmlDocument
and XmlElement
objects in the System.Xml
namespace.
For your purposes, constructing the desired XML will be easy with the XmlDocument
and XmlElement
but it will be more code. To create an XmlElement
you will need an XmlDocument
to create the object. The XmlDocument.CreateElement
function has an overload that allows you to specify both the namespace prefix and the namespace, so that is a little more straight forward.
One thing to note with using XmlDocument
and XmlElement
is you will need to manually append the XmlElement
to the appropriate object after it has been constructed. Another thing is you cannot specify the body of an XmlElement
during construction either, so that will need to be done after creation as well.
The order of the operations for the construction of the XML in this manner do not matter. It is mostly making sure you are appending the XmlElement
to the right parent, and setting the InnerText
on the right XmlElement
object.
var soapPrefix = "soapenv";
var soapNs = "http://schemas.xmlsoap.org/soap/envelope/";
var v1Prefix = "v1";
var v1Ns = "http://postilion/realtime/merchantframework/xsd/v1/";
var xmlDoc = new XmlDocument();
var envelope = xmlDoc.CreateElement(soapPrefix, "Envelope", soapNs);
xmlDoc.AppendChild(envelope);
var body = xmlDoc.CreateElement(soapPrefix, "Body", soapNs);
envelope.AppendChild(body);
var sendTranRequest = xmlDoc.CreateElement(v1Prefix, "SendTranRequest", v1Ns);
body.AppendChild(sendTranRequest);
var merc = xmlDoc.CreateElement(v1Prefix, "merc", v1Ns);
sendTranRequest.AppendChild(merc);
var idElement = xmlDoc.CreateElement(v1Prefix, "id", v1Ns);
idElement.InnerText = id.ToString();
merc.AppendChild(idElement);
var regKeyElement = xmlDoc.CreateElement(v1Prefix, "regKey", v1Ns);
regKeyElement.InnerText = regKey;
merc.AppendChild(regKeyElement);
var inTypeElement = xmlDoc.CreateElement(v1Prefix, "inType", v1Ns);
inTypeElement.InnerText = inType.ToString();
merc.AppendChild(inTypeElement);
var prodTypeElement = xmlDoc.CreateElement(v1Prefix, "prodType", v1Ns);
prodTypeElement.InnerText = prodType.ToString();
merc.AppendChild(prodTypeElement);
var tranCodeElement = xmlDoc.CreateElement(v1Prefix, "tranCode", v1Ns);
tranCodeElement.InnerText = tranCode.ToString();
sendTranRequest.AppendChild(tranCodeElement);
This results in the same XML as above.
When using an XmlDocument
to construct your XML request, there are two XmlDocument.Save
functions that you could use for your purposes. One accepts a Stream
the other accepts a XmlWriter
. If you can omit whitespace use the overload that accepts a XmlWriter
and adding that to the HttpWebRequest
would be:
using (var requestStream = httpRequest.GetRequestStream())
using (var writer = XmlWriter.Create(requestStream, xmlWriterSettings))
{
xmlDoc.Save(writer);
}
If you need to include the whitespace use:
using (var requestStream = httpRequest.GetRequestStream())
{
xmlDoc.Save(requestStream);
}