[Cleanup] Removed TARGET_JVM
[mono-project.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebClientProtocol.cs
blob59bb8bfbd51c02958697cedbca5ff4080ff9f12e
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 static HybridDictionary cache;
63 #endregion
65 #region Constructors
67 static WebClientProtocol ()
69 cache = new HybridDictionary ();
72 protected WebClientProtocol ()
74 connectionGroupName = String.Empty;
75 credentials = null;
76 preAuthenticate = false;
77 requestEncoding = null;
78 timeout = 100000;
81 #endregion // Constructors
83 #region Properties
85 [DefaultValue ("")]
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.")]
114 public int Timeout {
115 get { return timeout; }
116 set { timeout = value; }
119 [DefaultValue ("")]
120 [RecommendedAsConfigurable (true)]
121 [WebServicesDescription ("The base URL to the server to use for requests.")]
122 public string Url {
123 get { return uri == null ? String.Empty : uri.AbsoluteUri; }
124 set { uri = new Uri (value); }
126 #if NET_2_0
127 public bool UseDefaultCredentials {
128 get { return CredentialCache.DefaultCredentials == Credentials; }
129 set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
131 #endif
133 #endregion // Properties
135 #region Methods
137 public virtual void Abort ()
139 WebRequest request = current_request;
140 current_request = null;
141 if (request != null)
142 request.Abort ();
145 protected static void AddToCache (Type type, object value)
147 cache [type] = value;
150 protected static object GetFromCache (Type type)
152 return cache [type];
155 protected virtual WebRequest GetWebRequest (Uri uri)
157 if (uri == null)
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;
169 return request;
172 protected virtual WebResponse GetWebResponse (WebRequest request)
174 WebResponse response = null;
175 try {
176 request.Timeout = timeout;
177 response = request.GetResponse ();
178 } catch (WebException e) {
179 response = e.Response;
180 if (response == null)
181 throw;
184 return response;
187 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
189 return request.EndGetResponse (result);
192 #endregion // Methods