static void Main(string[] args) { string Server = "<server>"; string Username = "******"; string Password = "******"; using (var obixClient = new XmlObixClient(new Uri(Server))) { obixClient.WebClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ( "Basic", Convert.ToBase64String( Encoding.ASCII.GetBytes( string.Format("{0}:{1}", Username, Password)))); var connectResult = obixClient.Connect(); if (connectResult != ObixResult.kObixClientSuccess) { throw new Exception("Connection to server failed: " + ObixResult.Message(connectResult)); } var readResult = obixClient.ReadUriXml(new Uri(Server + "histories/")); if (readResult.ResultSucceeded) { var element = readResult.Result; System.Console.WriteLine(element.ToString()); } else { throw new Exception("Error reading from server: " + ObixResult.Message(readResult)); } System.Console.ReadKey(); } }
/// <summary> /// Invokes the obix:op(eration) at endpoint <paramref name="uri"/> with optional parameters specified by <paramref name="element"/>. /// </summary> /// <param name="uri">URI of the obix:op</param> /// <param name="element">An oBIX object to send as parameters to the URI. If null, no parameters are sent.</param> /// <returns>kObixClientResultSuccess on success with the result of the obix:op as an XElement, another value otherwise.</returns> public ObixResult <XElement> InvokeUriXml(Uri uri, XElement element) { XmlWriter writer = null; XmlReader reader = null; XElement responseElement = null; XDocument doc = null; MemoryStream ms = null; byte[] request = null; ObixResult <byte[]> response = null; if (uri == null) { return(ErrorStack.PushWithObject(GetType(), ObixResult.kObixClientInputError, "Uri is nothing.", (XElement)null)); } if (element != null) { using (ms = new MemoryStream()) { using (writer = XmlWriter.Create(ms)) { element.WriteTo(writer); } request = ms.ToArray(); } } response = InvokeUri(uri, request); if (response.ResultSucceeded == false) { return(ErrorStack.PushWithObject(GetType(), response, "WriteUri failed to invoke operation: " + uri.ToString() + ": " + ObixResult.Message(response), (XElement)null)); } try { using (ms = new MemoryStream(response.Result)) { using (reader = XmlReader.Create(ms)) { doc = XDocument.Load(reader); } } } catch (Exception ex) { return(ErrorStack.PushWithObject(GetType(), ObixResult.kObixClientXMLParseError, "Could not parse the XML response from the server: " + ex.ToString(), (XElement)null)); } responseElement = doc.Root; if (responseElement.IsObixErrorContract() == true) { return(ErrorStack.PushWithObject(GetType(), ObixResult.kObixServerError, responseElement.ObixDisplay(), responseElement)); } return(ObixResult.FromResult(ObixResult.kObixClientSuccess, responseElement)); }
public async Task TestConnectAsync() { ObixResult result; Assert.IsNotNull(client, "Initializing the oBIX client failed."); result = await client.ConnectAsync(); Console.Out.WriteLine("Connect result: {0}", result); Assert.AreEqual <int>(result, ObixResult.kObixClientSuccess, string.Format("Connect failed with error: {0}: {1}", result, ObixResult.Message((int)result))); }
public void TestConnect() { ObixResult result; result = client.Connect(); Console.Out.WriteLine("Connect result: {0}", result); Assert.AreEqual <int>(result, ObixResult.kObixClientSuccess, string.Format("Connect failed with error: {0}: {1}", result, ObixResult.Message((int)result))); }