class OCS
{
public static void CallWebService()
{
try
{
var _url = "http://test-url/ocsinterface";
var _action = "http://test-url/ocsinterface?op=get_computers_V1";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
catch (Exception e)
{
throw e;
}
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(@"<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
<get_computers_V1>
<REQUEST>
<ENGINE>FIRST</ENGINE>
<ASKING_FOR>META</ASKING_FOR>
<CHECKSUM>131071</CHECKSUM>
<OFFSET>0</OFFSET>
<WANTED>131071</WANTED>
</REQUEST>
</get_computers_V1>
</soap:Body>
</soap:Envelope>
");
return soapEnvelop;
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = WebRequestMethods.Http.Post;
webRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
webRequest.Credentials = new NetworkCredential("login", "pass");
return webRequest;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
soapEnvelopeXml.Save(stream);
}
}