* Methods.cs: removed handler for UnknownNode event
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebClientProtocol.cs
1 // \r
2 // System.Web.Services.Protocols.WebClientProtocol.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //\r
7 // Copyright (C) Tim Coleman, 2002\r
8 //\r
9 \r
10 using System.Collections.Specialized;\r
11 using System.ComponentModel;\r
12 using System.Net;\r
13 using System.Text;\r
14 using System.Threading;\r
15 using System.Web.Services;\r
16 \r
17 namespace System.Web.Services.Protocols {\r
18         public abstract class WebClientProtocol : Component {\r
19 \r
20                 #region Fields\r
21 \r
22                 string connectionGroupName;\r
23                 ICredentials credentials;\r
24                 bool preAuthenticate;\r
25                 Encoding requestEncoding;\r
26                 int timeout;\r
27                 string url;\r
28                 bool abort;\r
29 \r
30                 //\r
31                 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.\r
32                 //\r
33                 protected internal Uri uri;\r
34                         \r
35                 //\r
36                 // Points to the current request, so we can call Abort() on it\r
37                 //\r
38                 WebRequest current_request;\r
39                 \r
40                 static HybridDictionary cache;\r
41                 #endregion\r
42 \r
43                 #region Constructors\r
44 \r
45                 static WebClientProtocol ()\r
46                 {\r
47                         cache = new HybridDictionary ();\r
48                 }\r
49 \r
50                 protected WebClientProtocol () \r
51                 {\r
52                         connectionGroupName = String.Empty;\r
53                         credentials = null;\r
54                         preAuthenticate = false;\r
55                         requestEncoding = null;\r
56                         timeout = 100000;\r
57                         url = String.Empty;\r
58                         abort = false;\r
59                 }\r
60                 \r
61                 #endregion // Constructors\r
62 \r
63                 #region Properties\r
64 \r
65                 [DefaultValue ("")]\r
66                 public string ConnectionGroupName {\r
67                         get { return connectionGroupName; }\r
68                         set { connectionGroupName = value; }\r
69                 }\r
70 \r
71                 public ICredentials Credentials {\r
72                         get { return credentials; }\r
73                         set { credentials = value; }\r
74                 }\r
75 \r
76                 [DefaultValue (false)]\r
77                 [WebServicesDescription ("Enables pre authentication of the request.")]\r
78                 public bool PreAuthenticate {\r
79                         get { return preAuthenticate; }\r
80                         set { preAuthenticate = value; }\r
81                 }\r
82 \r
83                 [DefaultValue ("")]\r
84                 [RecommendedAsConfigurable (true)]\r
85                 [WebServicesDescription ("The encoding to use for requests.")]\r
86                 public Encoding RequestEncoding {\r
87                         get { return requestEncoding; }\r
88                         set { requestEncoding = value; }\r
89                 }\r
90 \r
91                 [DefaultValue (100000)]\r
92                 [RecommendedAsConfigurable (true)]\r
93                 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls.  The default of -1 means infinite.")]\r
94                 public int Timeout {\r
95                         get { return timeout; }\r
96                         set { timeout = value; }\r
97                 }\r
98 \r
99                 [DefaultValue ("")]\r
100                 [RecommendedAsConfigurable (true)]\r
101                 [WebServicesDescription ("The base URL to the server to use for requests.")]\r
102                 public string Url {\r
103                         get { return url; }\r
104                         set {\r
105                                 url = value;\r
106                                 uri = new Uri (url);\r
107                         }\r
108                 }\r
109 \r
110                 #endregion // Properties\r
111 \r
112                 #region Methods\r
113 \r
114                 public virtual void Abort ()\r
115                 {\r
116                         if (current_request != null){\r
117                                 current_request.Abort ();\r
118                                 current_request = null;\r
119                         }\r
120                         abort = true;\r
121                 }\r
122 \r
123                 protected static void AddToCache (Type type, object value)\r
124                 {\r
125                         cache [type] = value;\r
126                 }\r
127 \r
128                 protected static object GetFromCache (Type type)\r
129                 {\r
130                         return cache [type];\r
131                 }\r
132 \r
133                 protected virtual WebRequest GetWebRequest (Uri uri)\r
134                 {\r
135                         current_request = WebRequest.Create (uri);\r
136                         current_request.Timeout = Timeout;\r
137 \r
138                         if (credentials != null)\r
139                                 current_request.Credentials = credentials;\r
140                         if (connectionGroupName != String.Empty)\r
141                                 current_request.ConnectionGroupName = connectionGroupName;\r
142 \r
143                         return current_request;\r
144                 }\r
145 \r
146                 protected virtual WebResponse GetWebResponse (WebRequest request)\r
147                 {\r
148                         if (abort)\r
149                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
150                         return request.GetResponse ();\r
151                 }\r
152 \r
153                 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
154                 {\r
155                         if (abort)\r
156                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
157 \r
158                         IAsyncResult ar = request.BeginGetResponse (null, null);\r
159                         ar.AsyncWaitHandle.WaitOne ();\r
160                         return request.EndGetResponse (result);\r
161                 }\r
162 \r
163                 #endregion // Methods\r
164         }\r
165 }\r