2002-07-23 Tim Coleman <tim@timcoleman.com>
[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                 static HybridDictionary cache;\r
30 \r
31                 #endregion\r
32 \r
33                 #region Constructors\r
34 \r
35                 static WebClientProtocol ()\r
36                 {\r
37                         cache = new HybridDictionary ();\r
38                 }\r
39 \r
40                 protected WebClientProtocol () \r
41                 {\r
42                         connectionGroupName = String.Empty;\r
43                         credentials = null;\r
44                         preAuthenticate = false;\r
45                         requestEncoding = null;\r
46                         timeout = 100000;\r
47                         url = String.Empty;\r
48                         abort = false;\r
49                 }\r
50                 \r
51                 #endregion // Constructors\r
52 \r
53                 #region Properties\r
54 \r
55                 [DefaultValue ("")]\r
56                 public string ConnectionGroupName {\r
57                         get { return connectionGroupName; }\r
58                         set { connectionGroupName = value; }\r
59                 }\r
60 \r
61                 public ICredentials Credentials {\r
62                         get { return credentials; }\r
63                         set { credentials = value; }\r
64                 }\r
65 \r
66                 [DefaultValue (false)]\r
67                 [WebServicesDescription ("Enables pre authentication of the request.")]\r
68                 public bool PreAuthenticate {\r
69                         get { return preAuthenticate; }\r
70                         set { preAuthenticate = value; }\r
71                 }\r
72 \r
73                 [DefaultValue ("")]\r
74                 [RecommendedAsConfigurable (true)]\r
75                 [WebServicesDescription ("The encoding to use for requests.")]\r
76                 public Encoding RequestEncoding {\r
77                         get { return requestEncoding; }\r
78                         set { requestEncoding = value; }\r
79                 }\r
80 \r
81                 [DefaultValue (100000)]\r
82                 [RecommendedAsConfigurable (true)]\r
83                 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls.  The default of -1 means infinite.")]\r
84                 public int Timeout {\r
85                         get { return timeout; }\r
86                         set { timeout = value; }\r
87                 }\r
88 \r
89                 [DefaultValue ("")]\r
90                 [RecommendedAsConfigurable (true)]\r
91                 [WebServicesDescription ("The base URL to the server to use for requests.")]\r
92                 public string Url {\r
93                         get { return url; }\r
94                         set { url = value; }\r
95                 }\r
96 \r
97                 #endregion // Properties\r
98 \r
99                 #region Methods\r
100 \r
101                 public virtual void Abort ()\r
102                 {\r
103                         abort = true;\r
104                 }\r
105 \r
106                 protected static void AddToCache (Type type, object value)\r
107                 {\r
108                         cache [type] = value;\r
109                 }\r
110 \r
111                 protected static object GetFromCache (Type type)\r
112                 {\r
113                         return cache [type];\r
114                 }\r
115 \r
116                 protected virtual WebRequest GetWebRequest (Uri uri)\r
117                 {\r
118                         return WebRequest.Create (uri);\r
119                 }\r
120 \r
121                 protected virtual WebResponse GetWebResponse (WebRequest request)\r
122                 {\r
123                         if (abort)\r
124                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
125                         return request.GetResponse ();\r
126                 }\r
127 \r
128                 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
129                 {\r
130                         if (abort)\r
131                                 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
132 \r
133                         IAsyncResult ar = request.BeginGetResponse (null, null);\r
134                         ar.AsyncWaitHandle.WaitOne ();\r
135                         return request.EndGetResponse (result);\r
136                 }\r
137 \r
138                 #endregion // Methods\r
139         }\r
140 }\r