1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Text; |
---|
4 | using System.Net; |
---|
5 | using System.IO; |
---|
6 | |
---|
7 | using System.Web; |
---|
8 | using System.Xml.Serialization; |
---|
9 | using System.Xml; |
---|
10 | |
---|
11 | namespace Com.Piwigo.Lib.Proxy |
---|
12 | { |
---|
13 | |
---|
14 | static public class PwgGenericProxy<T> |
---|
15 | { |
---|
16 | |
---|
17 | public static T Get(Uri address, String methodName, String methodParameter) |
---|
18 | { |
---|
19 | T ReturnValue = default(T); |
---|
20 | HttpWebRequest Request; |
---|
21 | HttpWebResponse Response = null; |
---|
22 | StreamReader Reader; |
---|
23 | |
---|
24 | if (address == null) { throw new PwgProxyException("The get address is null."); } |
---|
25 | |
---|
26 | try |
---|
27 | { |
---|
28 | UriBuilder completeUri = new UriBuilder(address); |
---|
29 | if (methodParameter == null) |
---|
30 | { |
---|
31 | completeUri.Query = String.Format("method={0}", methodName); |
---|
32 | } |
---|
33 | else |
---|
34 | { |
---|
35 | completeUri.Query = String.Format("method={0}&{1}", methodName, methodParameter); |
---|
36 | } |
---|
37 | // Create and initialize the web request |
---|
38 | Request = WebRequest.Create(completeUri.Uri) as HttpWebRequest; |
---|
39 | Request.UserAgent = PwgConfigProxy.PwgUserAgent; |
---|
40 | Request.KeepAlive = PwgConfigProxy.PwgKeepAlive; |
---|
41 | Request.Timeout = PwgConfigProxy.PwgTimeOutInSecond * 1000; |
---|
42 | Request.CookieContainer = PwgConfigProxy.cookieContainer; // get session cookie |
---|
43 | |
---|
44 | // Get response |
---|
45 | Response = Request.GetResponse() as HttpWebResponse; |
---|
46 | |
---|
47 | if (Request.HaveResponse == true && Response != null) |
---|
48 | { |
---|
49 | // Get the response stream |
---|
50 | Reader = new StreamReader(Response.GetResponseStream()); |
---|
51 | |
---|
52 | ReturnValue = DeserializeObject(Reader); |
---|
53 | } |
---|
54 | } |
---|
55 | catch (WebException wex) |
---|
56 | { |
---|
57 | // This exception will be raised if the server didn't return 200 - OK |
---|
58 | // Try to retrieve more information about the network error |
---|
59 | if (wex.Response != null) |
---|
60 | { |
---|
61 | using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response) |
---|
62 | { |
---|
63 | throw new PwgProxyException( |
---|
64 | String.Format( |
---|
65 | "The server returned '{0}' with the status code {1} ({2:d}).", |
---|
66 | errorResponse.StatusDescription, errorResponse.StatusCode, |
---|
67 | errorResponse.StatusCode), |
---|
68 | wex); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | finally |
---|
73 | { |
---|
74 | if (Response != null) { Response.Close(); } |
---|
75 | } |
---|
76 | return ReturnValue; |
---|
77 | } |
---|
78 | |
---|
79 | public static T Post(Uri address, String data) |
---|
80 | { |
---|
81 | T ReturnValue = default(T); |
---|
82 | |
---|
83 | HttpWebRequest Request; |
---|
84 | HttpWebResponse Response = null; |
---|
85 | |
---|
86 | if (address == null) { throw new PwgProxyException("The get address is null."); } |
---|
87 | |
---|
88 | try |
---|
89 | { |
---|
90 | // Create the web request |
---|
91 | Request = WebRequest.Create(address) as HttpWebRequest; |
---|
92 | Request.UserAgent = PwgConfigProxy.PwgUserAgent; |
---|
93 | Request.KeepAlive = PwgConfigProxy.PwgKeepAlive; |
---|
94 | Request.Timeout = PwgConfigProxy.PwgTimeOutInSecond * 1000; |
---|
95 | Request.CookieContainer = PwgConfigProxy.cookieContainer; // get session cookie |
---|
96 | |
---|
97 | // Set type to POST |
---|
98 | Request.Method = "POST"; |
---|
99 | Request.ContentType = "application/x-www-form-urlencoded"; |
---|
100 | |
---|
101 | // Create a byte array of the data we want to send |
---|
102 | byte[] byteData = UTF8Encoding.UTF8.GetBytes(data); |
---|
103 | |
---|
104 | // Set the content length in the request headers |
---|
105 | Request.ContentLength = byteData.Length; |
---|
106 | |
---|
107 | // Write data |
---|
108 | using (Stream PostStream = Request.GetRequestStream()) |
---|
109 | { |
---|
110 | PostStream.Write(byteData, 0, byteData.Length); |
---|
111 | } |
---|
112 | |
---|
113 | // Get response |
---|
114 | using (Response = Request.GetResponse() as HttpWebResponse) |
---|
115 | { |
---|
116 | // Get the response stream |
---|
117 | StreamReader reader = new StreamReader(Response.GetResponseStream()); |
---|
118 | |
---|
119 | ReturnValue = DeserializeObject(reader); |
---|
120 | } |
---|
121 | } |
---|
122 | catch (WebException wex) |
---|
123 | { |
---|
124 | // This exception will be raised if the server didn't return 200 - OK |
---|
125 | // Try to retrieve more information about the network error |
---|
126 | if (wex.Response != null) |
---|
127 | { |
---|
128 | using (HttpWebResponse ErrorResponse = (HttpWebResponse)wex.Response) |
---|
129 | { |
---|
130 | throw new PwgProxyException( |
---|
131 | String.Format( |
---|
132 | "The server returned '{0}' with the status code {1} ({2:d}).", |
---|
133 | ErrorResponse.StatusDescription, ErrorResponse.StatusCode, |
---|
134 | ErrorResponse.StatusCode), |
---|
135 | wex); |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | finally |
---|
140 | { |
---|
141 | if (Response != null) { Response.Close(); } |
---|
142 | } |
---|
143 | return ReturnValue; |
---|
144 | } |
---|
145 | |
---|
146 | public static T DeserializeObject(StreamReader xmlData) |
---|
147 | { |
---|
148 | T ReturnValue = default(T); |
---|
149 | |
---|
150 | try |
---|
151 | { |
---|
152 | //Console.WriteLine(xmlData.ReadToEnd()); |
---|
153 | |
---|
154 | System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); |
---|
155 | ReturnValue = (T)xsSerializer.Deserialize(xmlData); |
---|
156 | } |
---|
157 | catch (Exception ex) |
---|
158 | { |
---|
159 | throw new PwgProxyException( |
---|
160 | String.Format( |
---|
161 | "The value {0} can't be deserialized in {1}, the error is {2} .", |
---|
162 | xmlData, typeof(T).Name, ex.Message), |
---|
163 | ex); |
---|
164 | } |
---|
165 | |
---|
166 | return ReturnValue; |
---|
167 | } |
---|
168 | |
---|
169 | public static void SerializeToXML(String fileName,T obj ) |
---|
170 | { |
---|
171 | try |
---|
172 | { |
---|
173 | XmlSerializer serializer = new XmlSerializer(typeof(T)); |
---|
174 | TextWriter textWriter = new StreamWriter(fileName); |
---|
175 | serializer.Serialize(textWriter, obj); |
---|
176 | textWriter.Close(); |
---|
177 | } |
---|
178 | catch (Exception ex) |
---|
179 | { |
---|
180 | throw new PwgProxyException( |
---|
181 | String.Format( |
---|
182 | "The value {0} can't be serialized in {1}, the error is {2} .", |
---|
183 | obj, fileName, ex.Message), |
---|
184 | ex); |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|