如何让 WebClient(webservice 客户端)自动使用默认代理服务器? [英] How can I get WebClient (webservice client) to automatically use the default proxy server?

查看:41
本文介绍了如何让 WebClient(webservice 客户端)自动使用默认代理服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 WinForms 应用程序调用网络服务.当不使用代理服务器时一切正常,但是当使用代理时,应用程序崩溃,而不是它期望的 SOAP 请求的 XML 响应,它会收到一个 HTML 错误页面,显示需要身份验证".

I'm calling a webservice from a WinForms app. 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".

您似乎可以像这样手动设置代理:

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;

...但在某种程度上,它似乎看到代理服务器无论如何没有执行上述操作,因为生成的错误实际上来自代理服务器.它似乎没有从用户的计算机上获取 Windows 身份验证登录凭据.我怎样才能强迫它这样做?

...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. How can I force it to do this?

在我自己的机器上,如果我使用 Fiddler(并启用需要代理身份验证"选项)进行模拟,我会弹出一个对话框,要求输入登录凭据,但这似乎不会发生在我客户端的机器上(谁使用真正的硬件代理 - McAfee Web Gateway).

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).

我该如何处理?我是否需要为用户提供一个对话框来手动配置服务器,或者是否有设置告诉 WebClient 使用 Windows 默认代理和用户自己的登录凭据?

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?

更新

似乎您可以使用下面的代码获取代理服务器,但这不会导致在所有情况下都出现身份验证对话框(适用于某些 PC,但不适用于其他 PC):

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):

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

如果上面的代码是正确的,我不明白为什么不会提示某些用户输入他们的凭据.我是否必须放入自己的代码来收集用户凭据并将其提供给 WebRequest 对象?

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?

推荐答案

尝试添加

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

到您的 app.config 文件

to your app.config file

这篇关于如何让 WebClient(webservice 客户端)自动使用默认代理服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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