If you ever need to reach a web service and use its output in your codewithout adding a web reference in your project, here is the code you can use:
I'm posting a code I used to connect to Netpoint Oblix CoreID Oracle Access Manager and retrieve values from it. This is a console app sample. You can use the same code in your web apps as well.
=====================================================
File Program.cs
=====================================================
using System;
using System.Xml;
namespace IDXML
{
class Program
{
private const string m_InFileName ="C:\\Projects\\IDXML\\IDXML\\InputXML.xml";
static void GetOblixData(string URL)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_InFileName);
Byte[] bdata =System.Text.Encoding.ASCII.GetBytes(xDoc.OuterXml);
System.Net.WebClient wc = newSystem.Net.WebClient();
wc.Headers.Add("Content-Type","text/xml");
Byte[] bresp;
bresp = wc.UploadData(URL, bdata);
string resp =System.Text.Encoding.ASCII.GetString(bresp);
//Console.WriteLine(resp);
XmlDocument xresp = new XmlDocument();
xresp.LoadXml(resp);
XmlNodeList elems =xresp.GetElementsByTagName("ObValue");
XmlNode x = elems[0]; // Assuming One / Picking First - ... If more then just loop
Console.WriteLine(x.InnerText);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message+""+ex.StackTrace);
}
}
static void Main(string[] args)
{
try
{
GetOblixData("http://localhost:8081/identity/oblix/apps/userservcenter/bin/userservcenter.cgi?wsdl");
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}
=====================================================
File Input.xml (I used this file to provide input xml sothat we can change it. You can simply prepare an xml in your code or read from somewhere else).
=====================================================
<?xml version="1.0"?>
<SOAP-ENV:Envelopexmlns:SOAP-ENV="http://schemas-xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<oblix:authenticationxmlns:oblix="http:/www.oblix.com" type="basic">
<oblix:login>admin</oblix:login>
<oblix:password>password</oblix:password>
</oblix:authentication>
<oblix:request application="userservcenter"function="view" version="NPWSDL1.0"xmlns:oblix="http://www.oblix.com">
<oblix:params>
<oblix:uid>cn=RajnishBhatia,o=NetscapeRoot</oblix:uid>
<oblix:attrName>mail</oblix:attrName>
</oblix:params>
</oblix:request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
=====================================================
Result
=====================================================
rajnishbhatia19@hotmail.com
Thursday, July 26, 2007
Calling A Web Service in .NET without adding a web reference
Posted by Rajnish Bhatia at 11:48 PM 2 comments
Subscribe to:
Posts (Atom)