views:

122

answers:

1

Hi guys,

An application I'm writing needs to use .Net Remoting (I'm aware that WCF is the 'new thing', but it is unfortunately not an option available to us at this time).

Anyway, everything works fine if I don't try to use the application through a proxy. However, the application needs to be able to function through proxy web servers. I set up a HttpChannel and set the 'proxyName' and 'proxyPort' properties to their correct values. This allows the channel to use the proxy server no problem.

I have the following problems:

1: If the proxy server requires authentication, there seems to be no way to set the credentials the channel should use to auth with the proxy. I've tried both the 'credentials' property, and the 'username' and 'password' properties, but it doesn't seem to work. So the end result in a case where the proxy server requires authentication, and just returns an authentication error whenever the remote method is called.

2: I cannot seem to get the HttpChannel to use the default system web proxy. If Internet Explorer is configured to use a proxy, I should just be able to use WebRequest.GetSystemWebProxy() to get it. However, this returns an IWebProxy, and I cannot extract the host and port from this. If anybody knows of a way to do this, I would greatly appreciate it.

What is frustrating is that if you step through the code using Visual Studio and examine the HttpChannel class, there is a WebProxy object. Life would be so much easier if they provided access to that!

So basically what I'm asking is how on earth do I get a HttpChannel to use a web proxy correctly - bearing in mind that I need to be able to use a proxy which requires authentication, and the ability to auto-detect the Internet Explorer proxy settings? Is there a simpler way to instantiate the channel so that I can just pass it a proxy object as a parameter?

Any help is much appreciated!

+2  A: 

I have found the solution to this problem (unfortunately I have lost the address of the blog which led me to this - if I can find it again I will give due credit), and it works perfectly. For anybody who is interested, add the following code:

    private static void SetChannelProxy(HttpClientChannel channel, IWebProxy proxy)
    {
        FieldInfo proxyObjectFieldInfo = typeof(HttpClientChannel).GetField("_proxyObject", BindingFlags.Instance | BindingFlags.NonPublic);

        proxyObjectFieldInfo.SetValue(channel, proxy);
    }

You should first configure your proxy object using the credentials you wish to use, and then call this method with the channel you want to use with the proxy.

TomFromThePool