From 80609c2b1572d9b946675db486fc46e327171cfa Mon Sep 17 00:00:00 2001 From: MW Date: Sun, 2 Dec 2007 13:59:15 +0000 Subject: Some refactoring , mainly on Inventory code. --- .../Framework/Servers/RestObjectPosterResponse.cs | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 OpenSim/Framework/Servers/RestObjectPosterResponse.cs (limited to 'OpenSim/Framework/Servers/RestObjectPosterResponse.cs') diff --git a/OpenSim/Framework/Servers/RestObjectPosterResponse.cs b/OpenSim/Framework/Servers/RestObjectPosterResponse.cs new file mode 100644 index 0000000..eeae285 --- /dev/null +++ b/OpenSim/Framework/Servers/RestObjectPosterResponse.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Xml; +using System.Xml.Serialization; + +namespace OpenSim.Framework.Servers +{ + public delegate void ReturnResponse(T reponse); + + public class RestObjectPosterResponse + { + public ReturnResponse ResponseCallback; + + public void BeginPostObject(string requestUrl, TRequest obj) + { + Type type = typeof(TRequest); + + WebRequest request = WebRequest.Create(requestUrl); + request.Method = "POST"; + request.ContentType = "text/xml"; + + MemoryStream buffer = new MemoryStream(); + + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; + + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, obj); + writer.Flush(); + } + + int length = (int)buffer.Length; + request.ContentLength = length; + + Stream requestStream = request.GetRequestStream(); + requestStream.Write(buffer.ToArray(), 0, length); + IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); + } + + private void AsyncCallback(IAsyncResult result) + { + WebRequest request = (WebRequest)result.AsyncState; + using (WebResponse resp = request.EndGetResponse(result)) + { + TResponse deserial; + XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); + deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); + + if (deserial != null && ResponseCallback != null) + { + ResponseCallback(deserial); + } + } + } + } +} -- cgit v1.1