**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebClientProtocol.cs
blob65f0f0852597b8e4e67d6541a1121529caa2a9e0
1 //
2 // System.Web.Services.Protocols.WebClientProtocol.cs
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Specialized;
32 using System.ComponentModel;
33 using System.Net;
34 using System.Text;
35 using System.Threading;
36 using System.Web.Services;
38 namespace System.Web.Services.Protocols {
39 public abstract class WebClientProtocol : Component {
41 #region Fields
43 string connectionGroupName;
44 ICredentials credentials;
45 bool preAuthenticate;
46 Encoding requestEncoding;
47 int timeout;
48 string url;
49 bool abort;
52 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
54 internal Uri uri;
57 // Points to the current request, so we can call Abort() on it
59 WebRequest current_request;
61 static HybridDictionary cache;
62 #endregion
64 #region Constructors
66 static WebClientProtocol ()
68 cache = new HybridDictionary ();
71 protected WebClientProtocol ()
73 connectionGroupName = String.Empty;
74 credentials = null;
75 preAuthenticate = false;
76 requestEncoding = null;
77 timeout = 100000;
78 url = String.Empty;
79 abort = false;
82 #endregion // Constructors
84 #region Properties
86 [DefaultValue ("")]
87 public string ConnectionGroupName {
88 get { return connectionGroupName; }
89 set { connectionGroupName = value; }
92 public ICredentials Credentials {
93 get { return credentials; }
94 set { credentials = value; }
97 [DefaultValue (false)]
98 [WebServicesDescription ("Enables pre authentication of the request.")]
99 public bool PreAuthenticate {
100 get { return preAuthenticate; }
101 set { preAuthenticate = value; }
104 [DefaultValue ("")]
105 [RecommendedAsConfigurable (true)]
106 [WebServicesDescription ("The encoding to use for requests.")]
107 public Encoding RequestEncoding {
108 get { return requestEncoding; }
109 set { requestEncoding = value; }
112 [DefaultValue (100000)]
113 [RecommendedAsConfigurable (true)]
114 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
115 public int Timeout {
116 get { return timeout; }
117 set { timeout = value; }
120 [DefaultValue ("")]
121 [RecommendedAsConfigurable (true)]
122 [WebServicesDescription ("The base URL to the server to use for requests.")]
123 public string Url {
124 get { return url; }
125 set {
126 url = value;
127 uri = new Uri (url);
131 #endregion // Properties
133 #region Methods
135 public virtual void Abort ()
137 if (current_request != null){
138 current_request.Abort ();
139 current_request = null;
141 abort = true;
144 protected static void AddToCache (Type type, object value)
146 cache [type] = value;
149 protected static object GetFromCache (Type type)
151 return cache [type];
154 protected virtual WebRequest GetWebRequest (Uri uri)
156 if (uri == null)
157 throw new InvalidOperationException ("uri is null");
159 current_request = WebRequest.Create (uri);
160 current_request.Timeout = timeout;
161 current_request.PreAuthenticate = preAuthenticate;
162 current_request.ConnectionGroupName = connectionGroupName;
164 if (credentials != null)
165 current_request.Credentials = credentials;
167 return current_request;
170 protected virtual WebResponse GetWebResponse (WebRequest request)
172 if (abort)
173 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
175 WebResponse response = null;
176 try {
177 request.Timeout = timeout;
178 response = request.GetResponse ();
179 } catch (WebException e) {
180 response = e.Response;
181 if (response == null)
182 throw;
185 return response;
188 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
190 if (abort)
191 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
193 return request.EndGetResponse (result);
196 #endregion // Methods