简体   繁体   English

如何让WebClient(Web服务客户端)自动使用默认代理服务器?

[英]How can I get WebClient (webservice client) to automatically use the default proxy server?

I'm calling a webservice from a WinForms app. 我正在从WinForms应用程序调用web服务。 Everything works fine when a proxy server isn't in use, however when a proxy is being used, the app crashes as instead of the XML response it's expecting for the SOAP request, it gets an HTML error page saying "Authentication Required". 当代理服务器未被使用时,一切正常,但是当使用代理时,应用程序崩溃而不是它对SOAP请求所期望的XML响应,它会得到一个HTML错误页面,上面写着“需要身份验证”。

It seems you can set the proxy manually like this: 您似乎可以手动设置代理,如下所示:

WebClient client = new WebClient();
WebProxy wp = new WebProxy("proxy server url here");
client.Proxy = wp;

...but to some extent, it seems to be seeing the proxy server anyway WITHOUT doing the above, as the error generated is actually coming from the proxy server. ...但在某种程度上,似乎无论如何都没有看到代理服务器而没有执行上述操作,因为生成的错误实际上来自代理服务器。 It just doesn't seem to be picking up the Windows Authentication login credentials from the user's computer. 它似乎没有从用户的计算机中获取Windows身份验证登录凭据。 How can I force it to do this? 我怎么强迫它这样做?

On my own machine if I simulate this using Fiddler (and enabling the "Require Proxy Authentication" option), I get a dialog pop up asking for the login credentials, but this doesn't seem to happen on my client's machines (who use a real hardware proxy - McAfee Web Gateway). 在我自己的机器上,如果我使用Fiddler模​​拟这个(并启用“需要代理身份验证”选项),我会弹出一个对话框询问登录凭据,但这似乎不会发生在我客户端的机器上(使用真正的硬件代理 - McAfee Web Gateway)。

How can I handle this? 我怎么处理这个? Do I need to provide a dialog for users to configure the server manually or is there a setting to tell WebClient to use the Windows default proxy and the user's own login credentials? 我是否需要为用户提供一个手动配置服务器的对话框,或者是否有设置告诉WebClient使用Windows默认代理和用户自己的登录凭据?

Update 更新

Seems like you can pick up the proxy server using the code below, but that doesn't cause the authentication dialog to appear in all situations (works on some PCs but not on others): 好像您可以使用下面的代码获取代理服务器,但这不会导致身份验证对话框出现在所有情况下(适用于某些PC但不适用于其他PC):

IWebProxy defaultProxy = WebRequest.DefaultWebProxy;
if (defaultProxy != null)
{
    defaultProxy.Credentials = CredentialCache.DefaultCredentials;
    client.Proxy = defaultProxy;
}

If the code above is correct, I don't understand why some users would not be prompted for their credentials. 如果上面的代码是正确的,我不明白为什么不会提示某些用户提供他们的凭据。 Do I have to put in my own code to collect the user credentials and supply them to the WebRequest object? 我是否必须输入自己的代码来收集用户凭据并将其提供给WebRequest对象?

Try adding 尝试添加

  <system.net>
    <defaultProxy useDefaultCredentials="true" />
  </system.net>

to your app.config file 到你的app.config文件

using (WebClient webClient = new WebClient())
{

    webClient.UseDefaultCredentials = true;
    webClient.Proxy = WebRequest.GetSystemWebProxy();
}

this should work 这应该工作

First try to use this: 首先尝试使用此:

WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
if (proxy.Address.AbsoluteUri != string.Empty)
{
    Console.WriteLine("Proxy URL: " + proxy.Address.AbsoluteUri);
    client.Proxy = proxy;
}

if this does not work try with: 如果这不起作用尝试:

WebProxy proxy = WebProxy.GetDefaultProxy()
client.Proxy = proxy;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM