2 // System.Web.Services.Protocols.WebClientProtocol.cs
\r
5 // Tim Coleman (tim@timcoleman.com)
\r
7 // Copyright (C) Tim Coleman, 2002
\r
10 using System.Collections.Specialized;
\r
11 using System.ComponentModel;
\r
14 using System.Threading;
\r
15 using System.Web.Services;
\r
17 namespace System.Web.Services.Protocols {
\r
18 public abstract class WebClientProtocol : Component {
\r
22 string connectionGroupName;
\r
23 ICredentials credentials;
\r
24 bool preAuthenticate;
\r
25 Encoding requestEncoding;
\r
31 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
\r
33 protected internal Uri uri;
\r
36 // Points to the current request, so we can call Abort() on it
\r
38 WebRequest current_request;
\r
40 static HybridDictionary cache;
\r
43 #region Constructors
\r
45 static WebClientProtocol ()
\r
47 cache = new HybridDictionary ();
\r
50 protected WebClientProtocol ()
\r
52 connectionGroupName = String.Empty;
\r
54 preAuthenticate = false;
\r
55 requestEncoding = null;
\r
61 #endregion // Constructors
\r
66 public string ConnectionGroupName {
\r
67 get { return connectionGroupName; }
\r
68 set { connectionGroupName = value; }
\r
71 public ICredentials Credentials {
\r
72 get { return credentials; }
\r
73 set { credentials = value; }
\r
76 [DefaultValue (false)]
\r
77 [WebServicesDescription ("Enables pre authentication of the request.")]
\r
78 public bool PreAuthenticate {
\r
79 get { return preAuthenticate; }
\r
80 set { preAuthenticate = value; }
\r
84 [RecommendedAsConfigurable (true)]
\r
85 [WebServicesDescription ("The encoding to use for requests.")]
\r
86 public Encoding RequestEncoding {
\r
87 get { return requestEncoding; }
\r
88 set { requestEncoding = value; }
\r
91 [DefaultValue (100000)]
\r
92 [RecommendedAsConfigurable (true)]
\r
93 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
\r
94 public int Timeout {
\r
95 get { return timeout; }
\r
96 set { timeout = value; }
\r
100 [RecommendedAsConfigurable (true)]
\r
101 [WebServicesDescription ("The base URL to the server to use for requests.")]
\r
102 public string Url {
\r
103 get { return url; }
\r
106 uri = new Uri (url);
\r
110 #endregion // Properties
\r
114 public virtual void Abort ()
\r
116 if (current_request != null){
\r
117 current_request.Abort ();
\r
118 current_request = null;
\r
123 protected static void AddToCache (Type type, object value)
\r
125 cache [type] = value;
\r
128 protected static object GetFromCache (Type type)
\r
130 return cache [type];
\r
133 protected virtual WebRequest GetWebRequest (Uri uri)
\r
135 current_request = WebRequest.Create (uri);
\r
136 current_request.Timeout = Timeout;
\r
138 if (credentials != null)
\r
139 current_request.Credentials = credentials;
\r
140 if (connectionGroupName != String.Empty)
\r
141 current_request.ConnectionGroupName = connectionGroupName;
\r
143 return current_request;
\r
146 protected virtual WebResponse GetWebResponse (WebRequest request)
\r
149 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
\r
150 return request.GetResponse ();
\r
153 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
\r
156 throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
\r
158 IAsyncResult ar = request.BeginGetResponse (null, null);
\r
159 ar.AsyncWaitHandle.WaitOne ();
\r
160 return request.EndGetResponse (result);
\r
163 #endregion // Methods
\r