public void Handle(string path, Stream request, Stream responseStream, OSHttpRequest httpRequest, OSHttpResponse httpResponse) { RestSessionObject <TRequest> deserial = default(RestSessionObject <TRequest>); bool fail = false; using (XmlTextReader xmlReader = new XmlTextReader(request)) { try { XmlSerializer deserializer = new XmlSerializer(typeof(RestSessionObject <TRequest>)); deserial = (RestSessionObject <TRequest>)deserializer.Deserialize(xmlReader); } catch (Exception e) { MainConsole.Instance.Error("[REST]: Deserialization problem. Ignoring request. " + e); fail = true; } } TResponse response = default(TResponse); if (!fail && m_smethod(deserial.SessionID, deserial.AvatarID)) { response = m_method(deserial.Body); } using (XmlWriter xmlWriter = XmlWriter.Create(responseStream)) { XmlSerializer serializer = new XmlSerializer(typeof(TResponse)); serializer.Serialize(xmlWriter, response); } }
public void BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid) { RestSessionObject <TRequest> sobj = new RestSessionObject <TRequest> { SessionID = sid, AvatarID = aid, Body = obj }; Type type = typeof(RestSessionObject <TRequest>); WebRequest request = WebRequest.Create(requestUrl); request.Method = verb; request.ContentType = "text/xml"; request.Timeout = 10000; MemoryStream buffer = new MemoryStream(); XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8 }; using (XmlWriter writer = XmlWriter.Create(buffer, settings)) { XmlSerializer serializer = new XmlSerializer(type); serializer.Serialize(writer, sobj); writer.Flush(); } buffer.Close(); int length = (int)buffer.Length; request.ContentLength = length; Stream requestStream = request.GetRequestStream(); requestStream.Write(buffer.ToArray(), 0, length); requestStream.Close(); // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); request.BeginGetResponse(AsyncCallback, request); }
public static TResponse BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid) { RestSessionObject <TRequest> sobj = new RestSessionObject <TRequest> { SessionID = sid, AvatarID = aid, Body = obj }; Type type = typeof(RestSessionObject <TRequest>); WebRequest request = WebRequest.Create(requestUrl); request.Method = verb; request.ContentType = "text/xml"; request.Timeout = 20000; MemoryStream buffer = new MemoryStream(); XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8 }; using (XmlWriter writer = XmlWriter.Create(buffer, settings)) { XmlSerializer serializer = new XmlSerializer(type); serializer.Serialize(writer, sobj); writer.Flush(); } int length = (int)buffer.Length; request.ContentLength = length; Stream requestStream = request.GetRequestStream(); requestStream.Write(buffer.ToArray(), 0, length); buffer.Close(); requestStream.Close(); TResponse deserial = default(TResponse); using (WebResponse resp = request.GetResponse()) { XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); Stream respStream = null; try { respStream = resp.GetResponseStream(); if (respStream != null) { deserial = (TResponse)deserializer.Deserialize(respStream); } } catch { } finally { if (respStream != null) { respStream.Close(); } resp.Close(); } } return(deserial); }