33 using System.Xml.Serialization;
35 namespace OpenSim.Framework.Servers.HttpServer
42 public class RestObjectPosterResponse<TResponse>
49 public void BeginPostObject<TRequest>(
string requestUrl, TRequest obj)
51 BeginPostObject(
"POST", requestUrl, obj);
54 public void BeginPostObject<TRequest>(
string verb,
string requestUrl, TRequest obj)
56 Type type = typeof (TRequest);
58 WebRequest request = WebRequest.Create(requestUrl);
59 request.Method = verb;
60 request.ContentType =
"text/xml";
61 request.Timeout = 10000;
63 using (MemoryStream buffer =
new MemoryStream())
65 XmlWriterSettings settings =
new XmlWriterSettings();
66 settings.Encoding = Encoding.UTF8;
68 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
70 XmlSerializer serializer =
new XmlSerializer(type);
71 serializer.Serialize(writer, obj);
75 int length = (int)buffer.Length;
76 request.ContentLength = length;
78 using (Stream requestStream = request.GetRequestStream())
79 requestStream.Write(buffer.ToArray(), 0, length);
83 request.BeginGetResponse(AsyncCallback, request);
86 private void AsyncCallback(IAsyncResult result)
88 WebRequest request = (WebRequest) result.AsyncState;
89 using (WebResponse resp = request.EndGetResponse(result))
92 XmlSerializer deserializer =
new XmlSerializer(typeof (TResponse));
93 Stream stream = resp.GetResponseStream();
99 deserial = (TResponse) deserializer.Deserialize(stream);
101 if (deserial != null && ResponseCallback != null)
103 ResponseCallback(deserial);
delegate void ReturnResponse< T >(T reponse)
ReturnResponse< TResponse > ResponseCallback