aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/RestObjectPosterResponse.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Servers/RestObjectPosterResponse.cs')
-rw-r--r--OpenSim/Framework/Servers/RestObjectPosterResponse.cs60
1 files changed, 60 insertions, 0 deletions
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 @@
1using System;
2using System.IO;
3using System.Net;
4using System.Text;
5using System.Xml;
6using System.Xml.Serialization;
7
8namespace OpenSim.Framework.Servers
9{
10 public delegate void ReturnResponse<T>(T reponse);
11
12 public class RestObjectPosterResponse<TResponse>
13 {
14 public ReturnResponse<TResponse> ResponseCallback;
15
16 public void BeginPostObject<TRequest>(string requestUrl, TRequest obj)
17 {
18 Type type = typeof(TRequest);
19
20 WebRequest request = WebRequest.Create(requestUrl);
21 request.Method = "POST";
22 request.ContentType = "text/xml";
23
24 MemoryStream buffer = new MemoryStream();
25
26 XmlWriterSettings settings = new XmlWriterSettings();
27 settings.Encoding = Encoding.UTF8;
28
29 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
30 {
31 XmlSerializer serializer = new XmlSerializer(type);
32 serializer.Serialize(writer, obj);
33 writer.Flush();
34 }
35
36 int length = (int)buffer.Length;
37 request.ContentLength = length;
38
39 Stream requestStream = request.GetRequestStream();
40 requestStream.Write(buffer.ToArray(), 0, length);
41 IAsyncResult result = request.BeginGetResponse(AsyncCallback, request);
42 }
43
44 private void AsyncCallback(IAsyncResult result)
45 {
46 WebRequest request = (WebRequest)result.AsyncState;
47 using (WebResponse resp = request.EndGetResponse(result))
48 {
49 TResponse deserial;
50 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
51 deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream());
52
53 if (deserial != null && ResponseCallback != null)
54 {
55 ResponseCallback(deserial);
56 }
57 }
58 }
59 }
60}