Muhammad Afzal | UCS XML API in C# ucs .net xml api xml ucs xml api c ucs manager visual studio c code Answer 11/16/12 1:08 AM While looking into UCS XML API documentation, I wrote a simple c# code as a test to consume UCS XML API in c# console application. I thought it would be worth sharing. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Xml; namespace UCSAPIConsoleApp { class Program { static void Main(string[] args) { string sAuthCookie, sUsername, sPassword, sURI; try { Console.Write("Enter UCS URI(http:///nuova: "); sURI = Console.ReadLine(); Console.Write("Enter username: "); sUsername = Console.ReadLine(); Console.Write("Enter password: "); sPassword = Console.ReadLine(); XmlDocument doc = new XmlDocument(); doc.LoadXml(HTTPRequestResponse(sURI, "")); XmlNode xmln = doc.SelectSingleNode("/aaaLogin"); sAuthCookie = xmln.Attributes["outCookie"].Value.ToString(); Console.WriteLine("Auth Cookie:{0}", sAuthCookie); Console.WriteLine("============================================================="); doc.LoadXml(HTTPRequestResponse(sURI, "")); xmln = doc.SelectSingleNode("/configResolveDn/outConfig/computeBlade"); for (int i = 0; i < xmln.Attributes.Count; i++) Console.WriteLine("{0}={1}", xmln.Attributes.Name, xmln.Attributes.Value); doc.LoadXml(HTTPRequestResponse(sURI, "")); xmln = doc.SelectSingleNode("/aaaLogout"); Console.WriteLine("============================================================="); Console.WriteLine("Logout={0}", xmln.Attributes["outStatus"].Value.ToString()); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine("Error occurred while execution:{0}", ex.Message); } } private static string HTTPRequestResponse(string sURL, string strXML) { byte[] XMLbuffer = Encoding.UTF8.GetBytes(strXML); try { HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(sURL); WebReq.Method = "POST"; WebReq.ContentType = "text/xml"; WebReq.ContentLength = XMLbuffer.Length; Stream PostData = WebReq.GetRequestStream(); PostData.Write(XMLbuffer, 0, XMLbuffer.Length); PostData.Close(); using (HttpWebResponse WebResponse = (HttpWebResponse)WebReq.GetResponse()) { Stream response = WebResponse.GetResponseStream(); return new StreamReader(response).ReadToEnd(); } } catch(Exception ex) { Console.WriteLine("Exception occurred: {0}", ex.Message); return null; } } } } OUTPUT: Auth Cookie:1353028019/a171253a-ebeb-4b21-b01d-4b357b611cba ============================================================= adminPower=policy adminState=in-service assignedToDn=org-root/ls-11 association=none availability=unavailable availableMemory=4096 chassisId=1 checkPoint=discovered connPath=A,B connStatus=A,B descr= discovery=complete dn=sys/chassis-1/blade-1 fltAggr=0 fsmDescr= fsmFlags= fsmPrev=DiscoverSuccess fsmProgr=100 fsmRmtInvErrCode=none fsmRmtInvErrDescr= fsmRmtInvRslt= fsmStageDescr= fsmStamp=2012-11-16T01:51:21.487 fsmStatus=nop fsmTry=0 intId=31838 lc=undiscovered lcTs=1970-01-01T01:00:00.000 lowVoltageMemory=not-applicable managingInst=A memorySpeed=not-applicable mfgTime=not-applicable model=N20-B6620-1 name= numOfAdaptors=1 numOfCores=6 numOfCoresEnabled=6 numOfCpus=1 numOfEthHostIfs=1 numOfFcHostIfs=0 numOfThreads=6 operPower=off operQualifier= operState=unassociated operability=operable originalUuid=1b4e28ba-2fa1-11d2-0101-b9a761bde3fb presence=equipped revision=0 serial=1045 serverId=1/1 slotId=1 totalMemory=4096 usrLbl= uuid=1b4e28ba-2fa1-11d2-0101-b9a761bde3fb vendor=Cisco Systems Inc ============================================================= Logout=success |
| Please sign in to flag this as inappropriate. |