473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get host/port from DefaultWebProxy?

5
Hi All,

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

Expand|Select|Wrap|Line Numbers
  1.        /// <summary>
  2.         /// Returns a WebProxy object based on active ProxyInfo and if Proxy is enabled, returns default system proxy otherwise
  3.         /// </summary>
  4.         public static IWebProxy GetProxySettings()
  5.         {
  6.             if (Program.conf.ProxyEnabled)
  7.             {
  8.                 ProxyInfo acc = Program.conf.ProxyActive;
  9.                 if (acc != null)
  10.                 {
  11.                     NetworkCredential cred = new NetworkCredential(acc.UserName, acc.Password);
  12.                     return new WebProxy(acc.GetAddress(), true, null, cred);
  13.                 }
  14.                 else
  15.                 {
  16.                     return HttpWebRequest.DefaultWebProxy;
  17.                 }
  18.             }
  19.             return null;
  20.         }
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
Aug 28 '09 #1
6 9708
tlhintoq
3,525 Expert 2GB
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.
Aug 28 '09 #2
McoreD
5
Thank you tlhintoq, fixed image. :)
Aug 28 '09 #3
JamieHowarth0
533 Expert 512MB
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.Address 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
Aug 29 '09 #4
McoreD
5
It would've been much simpler if there was a System.Net.HttpWebRequest.DefaultWebProxy.Address but unfortunately I can't seem to access this codegecko. :(
Aug 29 '09 #5
JamieHowarth0
533 Expert 512MB
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).

Expand|Select|Wrap|Line Numbers
  1. WebProxy x = CType(HttpWebRequest.DefaultWebProxy, WebProxy);
  2. int portno = Convert.ToInt32(x.Address.Uri.Port);
  3.  
Note: trying to cast using (WebProxy)HttpWebRequest.DefaultWebProxy 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
Aug 29 '09 #6
McoreD
5
Hi, unfortunately casting is possible in code, however fails during runtime. I have settled with the following try/catch workaround:

Expand|Select|Wrap|Line Numbers
  1.             IWebProxy proxy = Uploader.ProxySettings;
  2.             if (proxy != null)
  3.             {
  4.                 try
  5.                 {
  6.                     string testUrl = "http://google.com";
  7.                     Client.Proxy = new HttpProxyClient(proxy.GetProxy(new Uri(testUrl)).Host, proxy.GetProxy(new Uri(testUrl)).Port);
  8.                 }
  9.                 catch (Exception ex)
  10.                 {
  11.                     Console.WriteLine(ex.ToString());
  12.                 }
  13.             }
Aug 29 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
0
by: Ryan Stewart | last post by:
I'm running MySQL server on a WinXP box at home. I have a cable modem/router. I've set up port forwarding on port 3306 to the machine running MySQL. I can connect locally just fine, but when I try...
2
by: siliconmike | last post by:
I have a single instance of MySQL package installed on FreeBSD. I run 2 servers. The second server is run with this command: shell> mysqld_safe --defaults-extra-file=/root/my2.cnf & some...
6
by: MajorTom | last post by:
Hello, I getting this error An established connection was aborted by the software in your host machine here: TcpClient smtpSocket = new TcpClient(_serverSmtp, _portSmtp) port = 25 (smtp...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
3
by: nickyeng | last post by:
i know alot of people faced this problem before. i searched google.com i search javaranch.com, no one article can help me solve the problem. I runs it in my localhost server, it runs well. But...
1
by: malooga | last post by:
Hello, I'm having a problem connecting to DB2 on a remote iSeries host from a Linux server, both of which reside on my company's internal network. I'm using the IBM Linux Client V9.1. When I try...
3
by: =?ISO-8859-1?Q?Michael_Str=F6der?= | last post by:
HI! Is there a function in the standard lib which can be used to split a string containg 'host:port' into a tuple (host,port) and also does this reliably for IPv6 addresses? Ciao, Michael.
0
by: KartRacer88 | last post by:
Using pcAnywhere, I am trying to setup access to a pcAnywhere host PC that is behind two routers. The first router has a static public IP address on the internet. The second router has it's WAN...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.