add ISafeSerializationData
[mcs.git] / class / System.Web.Services / System.Web.Services.Protocols / WebClientProtocol.cs
bloba5c125388866aecf33cf48c8048254cf57189a08
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 #if NET_2_0
40 [System.Runtime.InteropServices.ComVisible (true)]
41 #endif
42 public abstract class WebClientProtocol : Component {
44 #region Fields
46 string connectionGroupName;
47 ICredentials credentials;
48 bool preAuthenticate;
49 Encoding requestEncoding;
50 int timeout;
53 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
55 internal Uri uri;
58 // Points to the current request, so we can call Abort() on it
60 WebRequest current_request;
62 #if !TARGET_JVM
63 static HybridDictionary cache;
64 #else
65 static HybridDictionary cache {
66 get {
67 return (HybridDictionary)AppDomain.CurrentDomain.GetData("WebClientProtocol.cache");
69 set {
70 AppDomain.CurrentDomain.SetData("WebClientProtocol.cache", value);
73 #endif
74 #endregion
76 #region Constructors
78 static WebClientProtocol ()
80 cache = new HybridDictionary ();
83 protected WebClientProtocol ()
85 connectionGroupName = String.Empty;
86 credentials = null;
87 preAuthenticate = false;
88 requestEncoding = null;
89 timeout = 100000;
92 #endregion // Constructors
94 #region Properties
96 [DefaultValue ("")]
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.")]
125 public int Timeout {
126 get { return timeout; }
127 set { timeout = value; }
130 [DefaultValue ("")]
131 [RecommendedAsConfigurable (true)]
132 [WebServicesDescription ("The base URL to the server to use for requests.")]
133 public string Url {
134 get { return uri == null ? String.Empty : uri.AbsoluteUri; }
135 set { uri = new Uri (value); }
137 #if NET_2_0
138 public bool UseDefaultCredentials {
139 get { return CredentialCache.DefaultCredentials == Credentials; }
140 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
142 #endif
144 #endregion // Properties
146 #region Methods
148 public virtual void Abort ()
150 WebRequest request = current_request;
151 current_request = null;
152 if (request != null)
153 request.Abort ();
156 protected static void AddToCache (Type type, object value)
158 cache [type] = value;
161 protected static object GetFromCache (Type type)
163 return cache [type];
166 protected virtual WebRequest GetWebRequest (Uri uri)
168 if (uri == null)
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;
180 return request;
183 protected virtual WebResponse GetWebResponse (WebRequest request)
185 WebResponse response = null;
186 try {
187 request.Timeout = timeout;
188 response = request.GetResponse ();
189 } catch (WebException e) {
190 response = e.Response;
191 if (response == null)
192 throw;
195 return response;
198 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
200 return request.EndGetResponse (result);
203 #endregion // Methods