本文整理汇总了C#中IWebProxy.GetProxy方法的典型用法代码示例。如果您正苦于以下问题:C# IWebProxy.GetProxy方法的具体用法?C# IWebProxy.GetProxy怎么用?C# IWebProxy.GetProxy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IWebProxy
的用法示例。
在下文中一共展示了IWebProxy.GetProxy方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCredentials
public ICredentials GetCredentials (Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
{
// if looking for proxy credentials, we care about the proxy's URL, not the request URL
if (credentialType == CredentialType.ProxyCredentials) {
var proxyUri = proxy.GetProxy (uri);
if (proxyUri != null)
uri = proxyUri;
}
lock (guiLock) {
// If this is the first attempt, return any stored credentials. If they fail, we'll be called again.
if (!retrying) {
var creds = GetExistingCredentials (uri, credentialType);
if (creds != null)
return creds;
}
return GetCredentialsFromUser (uri, proxy, credentialType);
}
}
示例2: GetCredentials
/// <summary>
/// Returns an ICredentials instance that the consumer would need in order
/// to properly authenticate to the given Uri.
/// </summary>
public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
// Capture the original proxy before we do anything
// so that we can re-set it once we get the credentials for the given Uri.
IWebProxy originalProxy = null;
if (proxy != null)
{
// If the current Uri should be bypassed then don't try to get the specific
// proxy but simply capture the one that is given to us
if (proxy.IsBypassed(uri))
{
originalProxy = proxy;
}
// If the current Uri is not bypassed then get a valid proxy for the Uri
// and make sure that we have the credentials also.
else
{
originalProxy = new WebProxy(proxy.GetProxy(uri));
originalProxy.Credentials = proxy.Credentials == null
? null : proxy.Credentials.GetCredential(uri, null);
}
}
try
{
// The cached credentials that we found are not valid so let's ask the user
// until they abort or give us valid credentials.
InitializeCredentialProxy(uri, originalProxy);
return PromptForCredentials(uri);
}
finally
{
// Reset the original WebRequest.DefaultWebProxy to what it was when we started credential discovery.
WebRequest.DefaultWebProxy = originalProxy;
}
}
示例3: SetWarp
public static void SetWarp(this WebRequest webRequest, WarpEngine warpEngine, IWebProxy proxy)
{
warpEngine.SetWarp(webRequest);
if (proxy != null)
{
// warproxy header
string base64String;
if (proxy is WebProxy)
{
base64String = Helper.FromProxy(proxy as WebProxy);
}
else
{
WebProxy webProxy = new WebProxy(proxy.GetProxy(webRequest.RequestUri));
webProxy.Credentials = proxy.Credentials.GetCredential(webRequest.RequestUri, "BASIC");
base64String = Helper.FromProxy(webProxy);
}
webRequest.Headers.Set("warproxy", base64String);
}
}
示例4: FindServicePoint
public static ServicePoint FindServicePoint (Uri address, IWebProxy proxy)
{
if (address == null)
throw new ArgumentNullException ("address");
RecycleServicePoints ();
bool usesProxy = false;
bool useConnect = false;
if (proxy != null && !proxy.IsBypassed(address)) {
usesProxy = true;
bool isSecure = address.Scheme == "https";
address = proxy.GetProxy (address);
if (address.Scheme != "http" && !isSecure)
throw new NotSupportedException ("Proxy scheme not supported.");
if (isSecure && address.Scheme == "http")
useConnect = true;
}
address = new Uri (address.Scheme + "://" + address.Authority);
ServicePoint sp = null;
lock (servicePoints) {
SPKey key = new SPKey (address, useConnect);
sp = servicePoints [key] as ServicePoint;
if (sp != null)
return sp;
if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
throw new InvalidOperationException ("maximum number of service points reached");
string addr = address.ToString ();
#if NET_2_1
int limit = defaultConnectionLimit;
#else
int limit = (int) manager.GetMaxConnections (addr);
#endif
sp = new ServicePoint (address, limit, maxServicePointIdleTime);
sp.Expect100Continue = expectContinue;
sp.UseNagleAlgorithm = useNagle;
sp.UsesProxy = usesProxy;
sp.UseConnect = useConnect;
sp.SetTcpKeepAlive (tcp_keepalive, tcp_keepalive_time, tcp_keepalive_interval);
servicePoints.Add (key, sp);
}
return sp;
}
示例5: MyGetWebData
private string MyGetWebData(Uri uri, string postData = null, CookieContainer cookies = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null, NameValueCollection headers = null, bool cache = true)
{
// do not use the cache when doing a POST
if (postData != null) cache = false;
// set a few headers if none were given
if (headers == null)
{
headers = new NameValueCollection();
headers.Add("Accept", "*/*"); // accept any content type
headers.Add("User-Agent", userAgent ?? OnlineVideoSettings.Instance.UserAgent); // set the default OnlineVideos UserAgent when none specified
}
if (referer != null) headers.Set("Referer", referer);
HttpWebResponse response = null;
try
{
// build a CRC of the url and all headers + proxy + cookies for caching
string requestCRC = Helpers.EncryptionUtils.CalculateCRC32(
string.Format("{0}{1}{2}{3}",
uri.ToString(),
headers != null ? string.Join("&", (from item in headers.AllKeys select string.Format("{0}={1}", item, headers[item])).ToArray()) : "",
proxy != null ? proxy.GetProxy(uri).AbsoluteUri : "",
cookies != null ? cookies.GetCookieHeader(uri) : ""));
// try cache first
string cachedData = cache ? WebCache.Instance[requestCRC] : null;
Log.Debug("GetWebData-{2}{1}: '{0}'", uri.ToString(), cachedData != null ? " (cached)" : "", postData != null ? "POST" : "GET");
if (cachedData != null) return cachedData;
// build the request
if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
if (request == null) return "";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; // turn on automatic decompression of both formats (adds header "AcceptEncoding: gzip,deflate" to the request)
if (cookies != null) request.CookieContainer = cookies; // set cookies if given
if (proxy != null) request.Proxy = proxy; // send the request over a proxy if given
if (headers != null) // set user defined headers
{
foreach (var headerName in headers.AllKeys)
{
switch (headerName.ToLowerInvariant())
{
case "accept":
request.Accept = headers[headerName];
break;
case "user-agent":
request.UserAgent = headers[headerName];
break;
case "referer":
request.Referer = headers[headerName];
break;
default:
request.Headers.Set(headerName, headers[headerName]);
break;
}
}
}
if (postData != null)
{
byte[] data = encoding != null ? encoding.GetBytes(postData) : Encoding.UTF8.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.ProtocolVersion = HttpVersion.Version10;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
// request the data
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException webEx)
{
Log.Debug(webEx.Message);
response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response
}
Stream responseStream = response.GetResponseStream();
// UTF8 is the default encoding as fallback
Encoding responseEncoding = Encoding.UTF8;
// try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim())) responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
// the caller did specify a forced encoding
if (encoding != null) responseEncoding = encoding;
// the caller wants to force UTF8
if (forceUTF8) responseEncoding = Encoding.UTF8;
using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
{
string str = reader.ReadToEnd().Trim();
// add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise)
if (cache && response.StatusCode == HttpStatusCode.OK && str.Length > 500) WebCache.Instance[requestCRC] = str;
return str;
}
}
finally
{
if (response != null) ((IDisposable)response).Dispose();
//.........这里部分代码省略.........
示例6: ConnectStream
/// <summary>
/// This function will connect a stream to a uri (host and port),
/// negotiating proxies and SSL
/// </summary>
/// <param name="uri"></param>
/// <param name="timeout_ms">Timeout, in ms. 0 for no timeout.</param>
/// <returns></returns>
public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int timeout_ms)
{
IMockWebProxy mockProxy = proxy != null ? proxy as IMockWebProxy : null;
if (mockProxy != null)
return mockProxy.GetStream(uri);
Stream stream;
bool useProxy = proxy != null && !proxy.IsBypassed(uri);
if (useProxy)
{
Uri proxyURI = proxy.GetProxy(uri);
stream = ConnectSocket(proxyURI, nodelay, timeout_ms);
}
else
{
stream = ConnectSocket(uri, nodelay, timeout_ms);
}
try
{
if (useProxy)
{
string line = String.Format("CONNECT {0}:{1} HTTP/1.0", uri.Host, uri.Port);
WriteLine(line, stream);
WriteLine(stream);
ReadHttpHeaders(ref stream, proxy, nodelay, timeout_ms);
}
if (UseSSL(uri))
{
SslStream sslStream = new SslStream(stream, false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
sslStream.AuthenticateAsClient("");
stream = sslStream;
}
return stream;
}
catch
{
stream.Close();
throw;
}
}
示例7: ProxyAddressIfNecessary
private static bool ProxyAddressIfNecessary(ref Uri address, IWebProxy proxy)
{
if (proxy != null && !address.IsLoopback)
{
Uri proxyAddress = proxy.GetProxy(address);
if (proxyAddress != null)
{
if (proxyAddress.Scheme != Uri.UriSchemeHttp)
{
throw new NotSupportedException(SR.Format(SR.net_proxyschemenotsupported, address.Scheme));
}
address = proxyAddress;
return true;
}
}
return false;
}
示例8: FindServicePoint
// If abortState becomes non-zero, the attempt to find a service point has been aborted.
internal static ServicePoint FindServicePoint(Uri address, IWebProxy proxy, out ProxyChain chain, ref HttpAbortDelegate abortDelegate, ref int abortState)
{
if (address==null) {
throw new ArgumentNullException("address");
}
GlobalLog.Enter("ServicePointManager::FindServicePoint() address:" + address.ToString());
bool isProxyServicePoint = false;
chain = null;
//
// find proxy info, and then switch on proxy
//
Uri proxyAddress = null;
if (proxy!=null && !address.IsLoopback) {
IAutoWebProxy autoProxy = proxy as IAutoWebProxy;
if (autoProxy != null)
{
chain = autoProxy.GetProxies(address);
// Set up our ability to abort this MoveNext call. Note that the current implementations of ProxyChain will only
// take time on the first call, so this is the only place we do this. If a new ProxyChain takes time in later
// calls, this logic should be copied to other places MoveNext is called.
GlobalLog.Assert(abortDelegate == null, "ServicePointManager::FindServicePoint()|AbortDelegate already set.");
abortDelegate = chain.HttpAbortDelegate;
try
{
Thread.MemoryBarrier();
if (abortState != 0)
{
Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
GlobalLog.LeaveException("ServicePointManager::FindServicePoint() Request aborted before proxy lookup.", exception);
throw exception;
}
if (!chain.Enumerator.MoveNext())
{
GlobalLog.Assert("ServicePointManager::FindServicePoint()|GetProxies() returned zero proxies.");
/*
Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestProhibitedByProxy), WebExceptionStatus.RequestProhibitedByProxy);
GlobalLog.LeaveException("ServicePointManager::FindServicePoint() Proxy prevented request.", exception);
throw exception;
*/
}
proxyAddress = chain.Enumerator.Current;
}
finally
{
abortDelegate = null;
}
}
else if (!proxy.IsBypassed(address))
{
// use proxy support
// rework address
proxyAddress = proxy.GetProxy(address);
}
// null means DIRECT
if (proxyAddress!=null) {
address = proxyAddress;
isProxyServicePoint = true;
}
}
ServicePoint servicePoint = FindServicePointHelper(address, isProxyServicePoint);
GlobalLog.Leave("ServicePointManager::FindServicePoint() servicePoint#" + ValidationHelper.HashString(servicePoint));
return servicePoint;
}
示例9: FindServicePoint
public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy)
{
if (address == null)
{
throw new ArgumentNullException("address");
}
var usesProxy = false;
if (proxy != null && !proxy.IsBypassed(address))
{
usesProxy = true;
var isSecure = address.Scheme == Uri.UriSchemeHttps;
address = proxy.GetProxy(address);
if (address.Scheme != Uri.UriSchemeHttp && !isSecure)
{
throw new NotSupportedException("Proxy scheme not supported.");
}
}
var key = MakeQueryString(address, usesProxy);
return _servicePoints.GetOrAdd(key, new Lazy<ServicePoint>(() =>
{
if (_maxServicePoints > 0 && _servicePoints.Count >= _maxServicePoints)
{
throw new InvalidOperationException("maximum number of service points reached");
}
return new ServicePoint(address, _defaultConnectionLimit, key, usesProxy);
}, false)).Value;
}
示例10: FindServicePoint
internal static ServicePoint FindServicePoint(Uri address, IWebProxy proxy, out ProxyChain chain, ref HttpAbortDelegate abortDelegate, ref int abortState)
{
if (address == null)
{
throw new ArgumentNullException("address");
}
bool isProxyServicePoint = false;
chain = null;
Uri current = null;
if ((proxy != null) && !address.IsLoopback)
{
IAutoWebProxy proxy2 = proxy as IAutoWebProxy;
if (proxy2 != null)
{
chain = proxy2.GetProxies(address);
abortDelegate = chain.HttpAbortDelegate;
try
{
Thread.MemoryBarrier();
if (abortState != 0)
{
Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
throw exception;
}
chain.Enumerator.MoveNext();
current = chain.Enumerator.Current;
}
finally
{
abortDelegate = null;
}
}
else if (!proxy.IsBypassed(address))
{
current = proxy.GetProxy(address);
}
if (current != null)
{
address = current;
isProxyServicePoint = true;
}
}
return FindServicePointHelper(address, isProxyServicePoint);
}
示例11: PrepareWebProxy
/// <summary>
/// This method attaches credentials to the web proxy object.
/// </summary>
/// <param name="proxy">The proxy to attach credentials to.</param>
/// <param name="webCallUrl">The url for the web call.</param>
/// <param name="oldProxyState">The current state fo the web call.</param>
/// <param name="newProxyState">The new state for the web call.</param>
/// <param name="okToPrompt">Prompt user for credentials if they are not available.</param>
public WebProxyState PrepareWebProxy(IWebProxy proxy, string webCallUrl, WebProxyState oldProxyState, bool okToPrompt)
{
WebProxyState newProxyState = WebProxyState.Abort;
if (string.IsNullOrEmpty(webCallUrl)) {
Debug.Fail("PrepareWebProxy called with an empty WebCallUrl.");
webCallUrl = "http://go.microsoft.com/fwlink/?LinkId=81947";
}
// Get the web proxy url for the the current web call.
Uri webCallProxy = null;
if (proxy != null) {
webCallProxy = proxy.GetProxy(new Uri(webCallUrl));
}
if ((proxy != null) && (webCallProxy != null)) {
// get proxy url.
string proxyUrl = webCallProxy.Host;
if (string.IsNullOrEmpty(currentProxyUrl)) {
currentProxyUrl = proxyUrl;
}
switch (oldProxyState) {
case WebProxyState.NoCredentials:
// Add the default credentials only if there aren't any credentials attached to
// the DefaultWebProxy. If the first calls attaches the correct credentials, the
// second call will just use them, instead of overwriting it with the default credentials.
// This avoids multiple web calls. Note that state is transitioned to DefaultCredentials
// instead of CachedCredentials. This ensures that web calls be tried with the
// cached credentials if the currently attached credentials don't result in successful web call.
if ((proxy.Credentials == null)) {
proxy.Credentials = CredentialCache.DefaultCredentials;
}
newProxyState = WebProxyState.DefaultCredentials;
break;
case WebProxyState.DefaultCredentials:
// Fetch cached credentials if they are null or if the proxy url has changed.
if ((cachedCredentials == null) ||
!string.Equals(currentProxyUrl, proxyUrl, StringComparison.OrdinalIgnoreCase)) {
cachedCredentials = GetCachedCredentials(proxyUrl);
}
if (cachedCredentials != null) {
proxy.Credentials = cachedCredentials;
newProxyState = WebProxyState.CachedCredentials;
break;
}
// Proceed to next step if cached credentials are not available.
goto case WebProxyState.CachedCredentials;
case WebProxyState.CachedCredentials:
case WebProxyState.PromptForCredentials:
if (okToPrompt) {
if (DialogResult.OK == PromptForCredentials(proxyUrl)) {
proxy.Credentials = cachedCredentials;
newProxyState = WebProxyState.PromptForCredentials;
} else {
newProxyState = WebProxyState.Abort;
}
} else {
newProxyState = WebProxyState.Abort;
}
break;
case WebProxyState.Abort:
throw new InvalidOperationException();
default:
throw new ArgumentException(string.Empty, "oldProxyState");
}
} else {
// No proxy for the webCallUrl scenario.
if (oldProxyState == WebProxyState.NoCredentials) {
// if it is the first call, change the state and let the web call proceed.
newProxyState = WebProxyState.DefaultCredentials;
} else {
Debug.Fail("This method is called a second time when 407 occurs. A 407 shouldn't have occurred as there is no default proxy.");
// We dont have a good idea of the circumstances under which
// WebProxy might be null for a url. To be safe, for VS 2005 SP1,
// we will just return the abort state, instead of throwing
// an exception. Abort state will ensure that no further procesing
// occurs and we will not bring down the app.
// throw new InvalidOperationException();
newProxyState = WebProxyState.Abort;
}
}
return newProxyState;
}
示例12: FindServicePoint
//
// FindServicePoint - Query using an Uri for a given server point
//
/// <include file='doc\ServicePointManager.uex' path='docs/doc[@for="ServicePointManager.FindServicePoint2"]/*' />
/// <devdoc>
/// <para>Findes an existing <see cref='System.Net.ServicePoint'/> or creates a new <see cref='System.Net.ServicePoint'/> to manage communications to the specified <see cref='System.Uri'/>
/// instance.</para>
/// </devdoc>
public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy) {
if (address==null) {
throw new ArgumentNullException("address");
}
GlobalLog.Enter("ServicePointManager::FindServicePoint() address:" + address.ToString());
string tempEntry;
bool isProxyServicePoint = false;
ScavengeIdleServicePoints();
//
// find proxy info, and then switch on proxy
//
if (proxy!=null && !proxy.IsBypassed(address)) {
// use proxy support
// rework address
Uri proxyAddress = proxy.GetProxy(address);
if (proxyAddress.Scheme != Uri.UriSchemeHttps && proxyAddress.Scheme != Uri.UriSchemeHttp) {
Exception exception = new NotSupportedException(SR.GetString(SR.net_proxyschemenotsupported, proxyAddress.Scheme));
GlobalLog.LeaveException("ServicePointManager::FindServicePoint() proxy has unsupported scheme:" + proxyAddress.Scheme.ToString(), exception);
throw exception;
}
address = proxyAddress;
isProxyServicePoint = true;
//
// Search for the correct proxy host,
// then match its acutal host by using ConnectionGroups
// which are located on the actual ServicePoint.
//
tempEntry = MakeQueryString(proxyAddress);
}
else {
//
// Typical Host lookup
//
tempEntry = MakeQueryString(address);
}
//
// lookup service point in the table
//
ServicePoint servicePoint = null;
lock (s_ServicePointTable) {
//
// once we grab the lock, check if it wasn't already added
//
WeakReference servicePointReference = (WeakReference) s_ServicePointTable[tempEntry];
if ( servicePointReference != null ) {
servicePoint = (ServicePoint)servicePointReference.Target;
}
if (servicePoint == null) {
//
// lookup failure or timeout, we need to create a new ServicePoint
//
if (s_MaxServicePoints<=0 || s_ServicePointTable.Count<s_MaxServicePoints) {
//
// Determine Connection Limit
//
int connectionLimit = InternalConnectionLimit;
string schemeHostPort = MakeQueryString(address);
if (ConfigTable.ContainsKey(schemeHostPort) ) {
connectionLimit = (int) ConfigTable[schemeHostPort];
}
// Note: we don't check permissions to access proxy.
// Rather, we should protect proxy property from being changed
servicePoint = new ServicePoint(address, s_MaxServicePointIdleTime, connectionLimit);
servicePointReference = new WeakReference(servicePoint);
// only set this when created, donates a proxy, statt Server
servicePoint.InternalProxyServicePoint = isProxyServicePoint;
s_ServicePointTable[tempEntry] = servicePointReference;
}
else {
Exception exception = new InvalidOperationException(SR.GetString(SR.net_maxsrvpoints));
GlobalLog.LeaveException("ServicePointManager::FindServicePoint() reached the limit count:" + s_ServicePointTable.Count.ToString() + " limit:" + s_MaxServicePoints.ToString(), exception);
throw exception;
}
}
} // lock
//.........这里部分代码省略.........
示例13: GetWebData
public static string GetWebData(string url, CookieContainer cc = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null)
{
HttpWebResponse response = null;
try
{
string requestCRC = Helpers.EncryptionUtils.CalculateCRC32(string.Format("{0}{1}{2}{3}{4}", url, referer, userAgent, proxy != null ? proxy.GetProxy(new Uri(url)).AbsoluteUri : "", cc != null ? cc.GetCookieHeader(new Uri(url)) : ""));
// try cache first
string cachedData = WebCache.Instance[requestCRC];
Log.Debug("GetWebData{1}: '{0}'", url, cachedData != null ? " (cached)" : "");
if (cachedData != null) return cachedData;
// request the data
if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
if (request == null) return "";
if (!String.IsNullOrEmpty(userAgent))
request.UserAgent = userAgent; // set specific UserAgent if given
else
request.UserAgent = OnlineVideoSettings.Instance.UserAgent; // set OnlineVideos default UserAgent
request.Accept = "*/*"; // we accept any content type
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); // we accept compressed content
request.Headers.Add("X-Requested-With: XMLHttpRequest");
request.Headers.Add("x-addr: 127.0.0.1");
if (!String.IsNullOrEmpty(referer)) request.Referer = referer; // set referer if given
if (cc != null) request.CookieContainer = cc; // set cookies if given
if (proxy != null) request.Proxy = proxy; // send the request over a proxy if given
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException webEx)
{
Log.Debug(webEx.Message);
response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response
}
Stream responseStream;
if (response == null) return "";
if (response.ContentEncoding.ToLower().Contains("gzip"))
responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
else if (response.ContentEncoding.ToLower().Contains("deflate"))
responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
else
responseStream = response.GetResponseStream();
// UTF8 is the default encoding as fallback
Encoding responseEncoding = Encoding.UTF8;
// try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim())) responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
// the caller did specify a forced encoding
if (encoding != null) responseEncoding = encoding;
// the caller wants to force UTF8
if (forceUTF8) responseEncoding = Encoding.UTF8;
using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
{
string str = reader.ReadToEnd().Trim();
// add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise)
if (response.StatusCode == HttpStatusCode.OK && str.Length > 500) WebCache.Instance[requestCRC] = str;
return str;
}
}
finally
{
if (response != null) ((IDisposable)response).Dispose();
// disable unsafe header parsing if it was enabled
if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false);
}
}
示例14: GetCredentials
public ICredentials GetCredentials (Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
{
if (credentialType != CredentialType.ProxyCredentials)
return wrapped.GetCredentials (uri, proxy, credentialType, retrying);
var proxyUri = proxy.GetProxy (uri);
if (proxyUri == null)
return null;
if (!retrying) {
var cached = CredentialStore.Instance.GetCredentials (proxyUri);
if (cached != null)
return cached;
}
lock (locker) {
if (!retrying) {
var cached = CredentialStore.Instance.GetCredentials (proxyUri);
if (cached != null)
return cached;
}
var creds = wrapped.GetCredentials (uri, proxy, credentialType, retrying);
if (creds != null)
CredentialStore.Instance.Add (proxyUri, creds);
return creds;
}
}