How to get host/port from DefaultWebProxy?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • McoreD
    New Member
    • Jul 2009
    • 5

    How to get host/port from DefaultWebProxy?

    Hi All,

    I understand HttpWebRequest. DefaultWebProxy returns you default port/host in a computer that it is connected to, if such proxy exists.

    Code:
           /// <summary>
            /// Returns a WebProxy object based on active ProxyInfo and if Proxy is enabled, returns default system proxy otherwise
            /// </summary>
            public static IWebProxy GetProxySettings()
            {
                if (Program.conf.ProxyEnabled)
                {
                    ProxyInfo acc = Program.conf.ProxyActive;
                    if (acc != null)
                    {
                        NetworkCredential cred = new NetworkCredential(acc.UserName, acc.Password);
                        return new WebProxy(acc.GetAddress(), true, null, cred);
                    }
                    else
                    {
                        return HttpWebRequest.DefaultWebProxy;
                    }
                }
                return null;
            }
    However, I have no clue how to get the host/port information and display it. I know in Quick Watch I can see this information.



    Thanks in advance,
    McoreD
    Last edited by McoreD; Aug 28 '09, 11:14 PM. Reason: bigger photo
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Your tiny thumbnail is unreadable. Can you post a bigger photo.

    If you can see the value in a variable of QuickWatch, then that is the variable you want to display.

    Comment

    • McoreD
      New Member
      • Jul 2009
      • 5

      #3
      Thank you tlhintoq, fixed image. :)

      Comment

      • JamieHowarth0
        Recognized Expert Contributor
        • May 2007
        • 537

        #4
        Hi McoreD,

        I think the Address property has a sub-property which allows you to break apart the address into protocol, address, IP and port (if I remember rightly). IntelliSense and QuickWatching HttpWebRequest. DefaultProxy.Ad dress should help you isolate it out - or you could always do string math and get all chars between a ":" and a "/" (or end of string if "/" isn't found).

        Hope this helps.

        codegecko

        Comment

        • McoreD
          New Member
          • Jul 2009
          • 5

          #5
          It would've been much simpler if there was a System.Net.Http WebRequest.Defa ultWebProxy.Add ress but unfortunately I can't seem to access this codegecko. :(

          Comment

          • JamieHowarth0
            Recognized Expert Contributor
            • May 2007
            • 537

            #6
            That's a bummar - the Intellisense above seems to indicate it's available from your QuickWatch/breakpoint. I'm not too familiar with WebProxy parts in .NET but I'll give this my best shot (I've just looked a few things up on MSDN so hopefully this'll work).

            Code:
            WebProxy x = CType(HttpWebRequest.DefaultWebProxy, WebProxy);
            int portno = Convert.ToInt32(x.Address.Uri.Port);
            Note: trying to cast using (WebProxy)HttpW ebRequest.Defau ltWebProxy won't work for some strange reason even though both classes derive from IWebProxy. This page on MSDN has some example code.
            If it doesn't work, try using a declared instance of HttpWebRequest rather than the static property.

            Anyway, a) hope this works and b) hope it helps.

            codegecko

            Comment

            • McoreD
              New Member
              • Jul 2009
              • 5

              #7
              Hi, unfortunately casting is possible in code, however fails during runtime. I have settled with the following try/catch workaround:

              Code:
                          IWebProxy proxy = Uploader.ProxySettings;
                          if (proxy != null)
                          {
                              try
                              {
                                  string testUrl = "http://google.com";
                                  Client.Proxy = new HttpProxyClient(proxy.GetProxy(new Uri(testUrl)).Host, proxy.GetProxy(new Uri(testUrl)).Port);
                              }
                              catch (Exception ex)
                              {
                                  Console.WriteLine(ex.ToString());
                              }
                          }

              Comment

              Working...