1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
//
// System.Web.Services.Protocols.WebClientProtocol.cs
//
// Author:
// Tim Coleman (tim@timcoleman.com)
//
// Copyright (C) Tim Coleman, 2002
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Specialized;
using System.ComponentModel;
using System.Net;
using System.Text;
using System.Threading;
using System.Web.Services;
namespace System.Web.Services.Protocols {
[System.Runtime.InteropServices.ComVisible (true)]
public abstract class WebClientProtocol : Component {
#region Fields
string connectionGroupName;
ICredentials credentials;
bool preAuthenticate;
Encoding requestEncoding;
int timeout;
//
// Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
//
internal Uri uri;
//
// Points to the current request, so we can call Abort() on it
//
WebRequest current_request;
static HybridDictionary cache;
#endregion
#region Constructors
static WebClientProtocol ()
{
cache = new HybridDictionary ();
}
protected WebClientProtocol ()
{
connectionGroupName = String.Empty;
credentials = null;
preAuthenticate = false;
requestEncoding = null;
timeout = 100000;
}
#endregion // Constructors
#region Properties
[DefaultValue ("")]
public string ConnectionGroupName {
get { return connectionGroupName; }
set { connectionGroupName = value; }
}
public ICredentials Credentials {
get { return credentials; }
set { credentials = value; }
}
[DefaultValue (false)]
[WebServicesDescription ("Enables pre authentication of the request.")]
public bool PreAuthenticate {
get { return preAuthenticate; }
set { preAuthenticate = value; }
}
[DefaultValue (null)]
[RecommendedAsConfigurable (true)]
[WebServicesDescription ("The encoding to use for requests.")]
public Encoding RequestEncoding {
get { return requestEncoding; }
set { requestEncoding = value; }
}
[DefaultValue (100000)]
[RecommendedAsConfigurable (true)]
[WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
public int Timeout {
get { return timeout; }
set { timeout = value; }
}
[DefaultValue ("")]
[RecommendedAsConfigurable (true)]
[WebServicesDescription ("The base URL to the server to use for requests.")]
public string Url {
get { return uri == null ? String.Empty : uri.AbsoluteUri; }
set { uri = new Uri (value); }
}
public bool UseDefaultCredentials {
get { return CredentialCache.DefaultCredentials == Credentials; }
set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
}
#endregion // Properties
#region Methods
public virtual void Abort ()
{
WebRequest request = current_request;
current_request = null;
if (request != null)
request.Abort ();
}
protected static void AddToCache (Type type, object value)
{
cache [type] = value;
}
protected static object GetFromCache (Type type)
{
return cache [type];
}
protected virtual WebRequest GetWebRequest (Uri uri)
{
if (uri == null)
throw new InvalidOperationException ("uri is null");
WebRequest request = WebRequest.Create (uri);
request.Timeout = timeout;
request.PreAuthenticate = preAuthenticate;
request.ConnectionGroupName = connectionGroupName;
if (credentials != null)
request.Credentials = credentials;
current_request = request;
return request;
}
protected virtual WebResponse GetWebResponse (WebRequest request)
{
WebResponse response = null;
try {
request.Timeout = timeout;
response = request.GetResponse ();
} catch (WebException e) {
response = e.Response;
if (response == null)
throw;
}
return response;
}
protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
{
return request.EndGetResponse (result);
}
#endregion // Methods
}
}
|