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
{
40 [System
.Runtime
.InteropServices
.ComVisible (true)]
42 public abstract class WebClientProtocol
: Component
{
46 string connectionGroupName
;
47 ICredentials credentials
;
49 Encoding requestEncoding
;
53 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
58 // Points to the current request, so we can call Abort() on it
60 WebRequest current_request
;
62 static HybridDictionary cache
;
67 static WebClientProtocol ()
69 cache
= new HybridDictionary ();
72 protected WebClientProtocol ()
74 connectionGroupName
= String
.Empty
;
76 preAuthenticate
= false;
77 requestEncoding
= null;
81 #endregion // Constructors
86 public string ConnectionGroupName
{
87 get { return connectionGroupName; }
88 set { connectionGroupName = value; }
91 public ICredentials Credentials
{
92 get { return credentials; }
93 set { credentials = value; }
96 [DefaultValue (false)]
97 [WebServicesDescription ("Enables pre authentication of the request.")]
98 public bool PreAuthenticate
{
99 get { return preAuthenticate; }
100 set { preAuthenticate = value; }
103 [DefaultValue (null)]
104 [RecommendedAsConfigurable (true)]
105 [WebServicesDescription ("The encoding to use for requests.")]
106 public Encoding RequestEncoding
{
107 get { return requestEncoding; }
108 set { requestEncoding = value; }
111 [DefaultValue (100000)]
112 [RecommendedAsConfigurable (true)]
113 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
115 get { return timeout; }
116 set { timeout = value; }
120 [RecommendedAsConfigurable (true)]
121 [WebServicesDescription ("The base URL to the server to use for requests.")]
123 get { return uri == null ? String.Empty : uri.AbsoluteUri; }
124 set { uri = new Uri (value); }
127 public bool UseDefaultCredentials
{
128 get { return CredentialCache.DefaultCredentials == Credentials; }
129 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
133 #endregion // Properties
137 public virtual void Abort ()
139 WebRequest request
= current_request
;
140 current_request
= null;
145 protected static void AddToCache (Type type
, object value)
147 cache
[type
] = value;
150 protected static object GetFromCache (Type type
)
155 protected virtual WebRequest
GetWebRequest (Uri uri
)
158 throw new InvalidOperationException ("uri is null");
160 WebRequest request
= WebRequest
.Create (uri
);
161 request
.Timeout
= timeout
;
162 request
.PreAuthenticate
= preAuthenticate
;
163 request
.ConnectionGroupName
= connectionGroupName
;
165 if (credentials
!= null)
166 request
.Credentials
= credentials
;
168 current_request
= request
;
172 protected virtual WebResponse
GetWebResponse (WebRequest request
)
174 WebResponse response
= null;
176 request
.Timeout
= timeout
;
177 response
= request
.GetResponse ();
178 } catch (WebException e
) {
179 response
= e
.Response
;
180 if (response
== null)
187 protected virtual WebResponse
GetWebResponse (WebRequest request
, IAsyncResult result
)
189 return request
.EndGetResponse (result
);
192 #endregion // Methods