2 // System.Web.Services.Protocols.WebClientProtocol.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2002
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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;
35 using System.Threading;
36 using System.Web.Services;
38 namespace System.Web.Services.Protocols {
39 [System.Runtime.InteropServices.ComVisible (true)]
40 public abstract class WebClientProtocol : Component {
44 string connectionGroupName;
45 ICredentials credentials;
47 Encoding requestEncoding;
51 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
56 // Points to the current request, so we can call Abort() on it
58 WebRequest current_request;
60 static HybridDictionary cache;
65 static WebClientProtocol ()
67 cache = new HybridDictionary ();
70 protected WebClientProtocol ()
72 connectionGroupName = String.Empty;
74 preAuthenticate = false;
75 requestEncoding = null;
79 #endregion // Constructors
84 public string ConnectionGroupName {
85 get { return connectionGroupName; }
86 set { connectionGroupName = value; }
89 public ICredentials Credentials {
90 get { return credentials; }
91 set { credentials = value; }
94 [DefaultValue (false)]
95 [WebServicesDescription ("Enables pre authentication of the request.")]
96 public bool PreAuthenticate {
97 get { return preAuthenticate; }
98 set { preAuthenticate = value; }
101 [DefaultValue (null)]
102 [RecommendedAsConfigurable (true)]
103 [WebServicesDescription ("The encoding to use for requests.")]
104 public Encoding RequestEncoding {
105 get { return requestEncoding; }
106 set { requestEncoding = value; }
109 [DefaultValue (100000)]
110 [RecommendedAsConfigurable (true)]
111 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
113 get { return timeout; }
114 set { timeout = value; }
118 [RecommendedAsConfigurable (true)]
119 [WebServicesDescription ("The base URL to the server to use for requests.")]
121 get { return uri == null ? String.Empty : uri.AbsoluteUri; }
122 set { uri = new Uri (value); }
124 public bool UseDefaultCredentials {
125 get { return CredentialCache.DefaultCredentials == Credentials; }
126 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
129 #endregion // Properties
133 public virtual void Abort ()
135 WebRequest request = current_request;
136 current_request = null;
141 protected static void AddToCache (Type type, object value)
143 cache [type] = value;
146 protected static object GetFromCache (Type type)
151 protected virtual WebRequest GetWebRequest (Uri uri)
154 throw new InvalidOperationException ("uri is null");
156 WebRequest request = WebRequest.Create (uri);
157 request.Timeout = timeout;
158 request.PreAuthenticate = preAuthenticate;
159 request.ConnectionGroupName = connectionGroupName;
161 if (credentials != null)
162 request.Credentials = credentials;
164 current_request = request;
168 protected virtual WebResponse GetWebResponse (WebRequest request)
170 WebResponse response = null;
172 request.Timeout = timeout;
173 response = request.GetResponse ();
174 } catch (WebException e) {
175 response = e.Response;
176 if (response == null)
183 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
185 return request.EndGetResponse (result);
188 #endregion // Methods