private string formXml(string username, string password, string destination, string message)
{
// create xml string
string xmlString = “<?xml version=\”1.0\” encoding=\”UTF-8\” ?> ” +
“<Request xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xsi:noNamespaceSchemaLocation=\”http://schema.2sms.us/1.0/0410_RequestSendMessage.xsd\” Version=\”1.0\”>” +
“<Identification>” +
“<UserID>”+username+”</UserID>” +
“<Password>”+password+”</Password>” +
“</Identification>” +
“<Service>” +
“<ServiceName>SendMessage</ServiceName>” +
“<ServiceDetail>” +
“<SingleMessage>” +
“<Destination>”+destination+”</Destination>” +
“<Text>”+message+”</Text>” +
“</SingleMessage>” +
“</ServiceDetail>” +
“</Service>” +
“</Request>”;
return xmlString;
}
private string sendXml(string xmlDoc)
{
string resultText=String.Empty;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(postURL);
request.Method=”POST”;
// uncomment if appropriate to route web request through proxy
//request.Proxy.Credentials = new NetworkCredential(“username”, “password”);
//request.Proxy.Credentials = new NetworkCredential(“username”, “password”, “domain”);
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(xmlDoc);
writer.Flush();
writer.Close();
try
{
XmlTextReader reader = new XmlTextReader(request.GetResponse().GetResponseStream());
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case”Error”:
string errorCode=String.Empty;
string errorReason=String.Empty;
while(!(reader.Name==”Error” &&reader.NodeType==XmlNodeType.EndElement))
{
reader.Read();
if (reader.NodeType==XmlNodeType.Element)
{
switch (reader.Name)
{
case “ErrorCode”:
reader.Read(); // this should take us to the text node
errorCode = reader.Value;
break;
case “ErrorReason”:
reader.Read();
errorReason = reader.Value;
break;
}
}
}
if (errorCode!=”00″)
{ // if the error code did not signify an OK status …
resultText = errorReason;
}
break;
case “Result”:
reader.Read();
resultText = reader.Value;
break;
}
}
}
reader.Close();
}
catch (Exception e)
{ // log the exception
resultText = “An error occurred”;
}
return resultText;
}