从默认的Web代理获取的URI [英] Get the URI from the default web proxy

查看:168
本文介绍了从默认的Web代理获取的URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序应该不会有代理和代理与身份验证 - 自动!它应该调用WCF服务。在这个例子中,实例名为客户端。我也用一个自写的类( proxyHelper )请求的凭据。

I'm writing a program that should work without proxy and with proxy with authentication - automatically! It should call a WCF service. In this example the instance is called client. I also use a self written class (proxyHelper) that requests the credentials.

     basicHttpBinding的连接= client.Endpoint.Binding作为basicHttpBinding的;
     connection.ProxyAddress = ???
     connection.UseDefaultWebProxy = FALSE;
     connection.BypassProxyOnLocal = FALSE;
     connection.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
     client.ClientCredentials.UserName.UserName = proxyHelper.Username;      client.ClientCredentials.UserName.Password = proxyHelper.Password;

BasicHttpBinding connection = client.Endpoint.Binding as BasicHttpBinding;
connection.ProxyAddress = ???
connection.UseDefaultWebProxy = false;
connection.BypassProxyOnLocal = false;
connection.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
client.ClientCredentials.UserName.UserName = proxyHelper.Username; client.ClientCredentials.UserName.Password = proxyHelper.Password;

我现在面临一个问题得到了ProxyAddress。如果我使用 HttpWebRequest.GetSystemWebProxy()来获得实际的定义的代理我看到在调试模式下的正确的代理服务器地址,但它是一个非公共财产。设置UseDefaultWebProxy真不工作,如果我添加代理服务器地址硬codeD并设置UseDefaultWebProxy为false,它工作正常。所以,我......怎么可以收集默认的Web代理服务器的地址是什么?

I'm facing a problem getting the ProxyAddress. If I use HttpWebRequest.GetSystemWebProxy() to get actual defined proxy I see in debug mode the correct proxy address but it's a non public property. Setting UseDefaultWebProxy to true doesn't work and if I add the proxy address hard-coded and set the UseDefaultWebProxy to false it works fine. So... how can I gather the address of the default web proxy?

推荐答案

代理有一个名为的 GetProxy 的,可用于让代理的URI。

The proxy has a method called GetProxy which can be used to get the Uri of the proxy.

下面是从MSDN的描述中的一个片段:

Here's a snippet of the description from MSDN:

该GetProxy方法返回的URI   该WebRequest实例用来   访问互联网资源。

The GetProxy method returns the URI that the WebRequest instance uses to access the Internet resource.

GetProxy比较目的地的   BypassList的内容,使用   IsBypassed方法。如果IsBypassed   返回true,GetProxy回报   目的地和WebRequest的   实例不使用代理   服务器。

GetProxy compares destination with the contents of BypassList, using the IsBypassed method. If IsBypassed returns true, GetProxy returns destination and the WebRequest instance does not use the proxy server.

如果目的地不在BypassList,   在WebRequest实例使用代理   服务器和地址属性   回来了。

If destination is not in BypassList, the WebRequest instance uses the proxy server and the Address property is returned.

您可以使用下面的code,以获得代理的详细信息。需要注意的是,你传递给GetProxy方法,URI是重要的,因为如果代理不绕过指定的URI它只会返回你的代理凭据。

You can use the following code to get the proxy details. Note that the Uri that you pass to the GetProxy method is important, as it will only return you the proxy credentials if the proxy isn't bypassed for the specified Uri.

var proxy = System.Net.HttpWebRequest.GetSystemWebProxy();

//gets the proxy uri, will only work if the request needs to go via the proxy 
//(i.e. the requested url isn't in the bypass list, etc)
Uri proxyUri = proxy.GetProxy(new Uri("http://www.google.com"));

proxyUri.Host.Dump();        // 10.1.100.112
proxyUri.AbsoluteUri.Dump(); // http://10.1.100.112:8080/

这篇关于从默认的Web代理获取的URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆