GlobalProxySelection.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // System.Net.GlobalProxySelection
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Configuration;
  10. using System.IO;
  11. using System.Runtime.Serialization;
  12. namespace System.Net
  13. {
  14. public class GlobalProxySelection
  15. {
  16. private static IWebProxy proxy;
  17. // Constructors
  18. public GlobalProxySelection() { }
  19. // Properties
  20. static IWebProxy GetProxy ()
  21. {
  22. if (proxy != null)
  23. return proxy;
  24. lock (typeof (GlobalProxySelection)) {
  25. if (proxy != null)
  26. return proxy;
  27. object p = ConfigurationSettings.GetConfig ("system.net/defaultProxy");
  28. if (p == null)
  29. p = new EmptyWebProxy ();
  30. proxy = (IWebProxy) p;
  31. }
  32. return proxy;
  33. }
  34. public static IWebProxy Select {
  35. get { return GetProxy (); }
  36. set {
  37. if (value == null)
  38. throw new ArgumentNullException ("GlobalProxySelection.Select",
  39. "null IWebProxy not allowed. Use GetEmptyWebProxy ()");
  40. lock (typeof (GlobalProxySelection))
  41. proxy = value;
  42. }
  43. }
  44. // Methods
  45. public static IWebProxy GetEmptyWebProxy()
  46. {
  47. // must return a new one each time, as the credentials
  48. // can be set
  49. return new EmptyWebProxy ();
  50. }
  51. // Internal Classes
  52. internal class EmptyWebProxy : IWebProxy {
  53. private ICredentials credentials = null;
  54. internal EmptyWebProxy () { }
  55. public ICredentials Credentials {
  56. get { return credentials; }
  57. set { credentials = value; }
  58. }
  59. public Uri GetProxy (Uri destination)
  60. {
  61. return destination;
  62. }
  63. public bool IsBypassed (Uri host)
  64. {
  65. return true; // pass directly to host
  66. }
  67. }
  68. }
  69. }