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
;
63 static HybridDictionary cache
;
65 static HybridDictionary cache
{
67 return (HybridDictionary
)AppDomain
.CurrentDomain
.GetData("WebClientProtocol.cache");
70 AppDomain
.CurrentDomain
.SetData("WebClientProtocol.cache", value);
78 static WebClientProtocol ()
80 cache
= new HybridDictionary ();
83 protected WebClientProtocol ()
85 connectionGroupName
= String
.Empty
;
87 preAuthenticate
= false;
88 requestEncoding
= null;
92 #endregion // Constructors
97 public string ConnectionGroupName
{
98 get { return connectionGroupName; }
99 set { connectionGroupName = value; }
102 public ICredentials Credentials
{
103 get { return credentials; }
104 set { credentials = value; }
107 [DefaultValue (false)]
108 [WebServicesDescription ("Enables pre authentication of the request.")]
109 public bool PreAuthenticate
{
110 get { return preAuthenticate; }
111 set { preAuthenticate = value; }
114 [DefaultValue (null)]
115 [RecommendedAsConfigurable (true)]
116 [WebServicesDescription ("The encoding to use for requests.")]
117 public Encoding RequestEncoding
{
118 get { return requestEncoding; }
119 set { requestEncoding = value; }
122 [DefaultValue (100000)]
123 [RecommendedAsConfigurable (true)]
124 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
126 get { return timeout; }
127 set { timeout = value; }
131 [RecommendedAsConfigurable (true)]
132 [WebServicesDescription ("The base URL to the server to use for requests.")]
134 get { return uri == null ? String.Empty : uri.AbsoluteUri; }
135 set { uri = new Uri (value); }
138 public bool UseDefaultCredentials
{
139 get { return CredentialCache.DefaultCredentials == Credentials; }
140 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
144 #endregion // Properties
148 public virtual void Abort ()
150 WebRequest request
= current_request
;
151 current_request
= null;
156 protected static void AddToCache (Type type
, object value)
158 cache
[type
] = value;
161 protected static object GetFromCache (Type type
)
166 protected virtual WebRequest
GetWebRequest (Uri uri
)
169 throw new InvalidOperationException ("uri is null");
171 WebRequest request
= WebRequest
.Create (uri
);
172 request
.Timeout
= timeout
;
173 request
.PreAuthenticate
= preAuthenticate
;
174 request
.ConnectionGroupName
= connectionGroupName
;
176 if (credentials
!= null)
177 request
.Credentials
= credentials
;
179 current_request
= request
;
183 protected virtual WebResponse
GetWebResponse (WebRequest request
)
185 WebResponse response
= null;
187 request
.Timeout
= timeout
;
188 response
= request
.GetResponse ();
189 } catch (WebException e
) {
190 response
= e
.Response
;
191 if (response
== null)
198 protected virtual WebResponse
GetWebResponse (WebRequest request
, IAsyncResult result
)
200 return request
.EndGetResponse (result
);
203 #endregion // Methods